Skip to content

Latest commit

 

History

History
 
 

oci-apigw-idcs-auth-basic

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 
 
 
 
 
 
 

API Gateway Basicauth function using Identity Cloud Service (IDCS)

This function provides verification of username and password against IDCS at runtime and allows only authorized users to access API gateway deployment.

The implementation conforms to the documented guidlines for using authorizer functions to add Authentication and Authorization to API deployments.

As you make your way through this tutorial, look out for this icon user input icon. Whenever you see it, it's time for you to perform an action.

Prerequisites

Create users in IDCS

Before you deploy this sample function, you need to complete steps A, B and C of the Oracle Functions Quick Start Guide for Cloud Shell

  • A - Set up your tenancy
  • B - Create application
  • C - Set up your Cloud Shell dev environment

List Applications

Assuming your have successfully completed the prerequisites, you should see your application in the list of applications.

fn ls apps

Deploy a function that implements an API

We need another function that will be a target for API Gateway. We suggest oci-display-httprequest-info-python. In Cloud Shell, run fn deploy to build the function and its dependencies as a container, push the image to Oracle Cloud Infrastructure Registry (OCIR), and deploy the function to Oracle Functions in your application.

user input icon

cd ../oci-display-httprequest-info-python
fn -v deploy --app <app-name>

Create or Update your Dynamic Group for API Gateway

In order to invoke functions, your API Gateway must be part of a dynamic group.

When specifying the Matching Rules, we suggest matching all functions in a compartment with:

ALL {resource.type = 'ApiGateway', resource.compartment.id = 'ocid1.compartment.oc1..aaaaaxxxxx'}

Create or Update IAM Policies for API Gateway

Create a new policy that allows the API Gateway dynamic group to invoke functions. We will grant use access to functions-family in the compartment.

user input icon

Your policy should look something like this:

Allow dynamic-group <dynamic-group-name> to use functions-family in compartment <compartment-name>

For more information on how to create policies, check the documentation.

Configure Identity Cloud Service (IDCS)

Login to IDCS admin console and create, add an Application and select "Confidential Application". IDCS-appcreate0

Enter a name for your IDCS Application, for example "myAPI".

IDCS-appcreate1

For "Allowed Grant Types", select "Resource Owner". Click Next.

IDCS-appcreate2

For Primary Audience, enter anything "display-httprequest-info" for example. For Scopes, click Add. In the dialog box, for field "Scope", enter anything "display-httprequest-info" for example, click Add.

IDCS-appcreate3

Click Next.

IDCS-appcreate4

Click Finish.

IDCS-appcreate5

Now that the application is added, note the Client ID and Client Secret.

IDCS-appcreate6

Click Close.

Click on Configurations tab under Client Information section click on add scope and select the application name from the dropdown. Note the scope value.

IDCS-appcreate7 IDCS-appcreate8

Click Activate and click Ok in the dialog.

Note the IDCS URL, this is the URL you see in your browser URL bar, copy the IDCS url ( For example: https://idcs-xxxxxxxxxxx.identity.oraclecloud.com/ ), client-id, client-secret and scope these values are provided to the Basicauth function.

Review and customize the function

Review the following files in the current folder:

The name of your function basicauth is specified in func.yaml.

set the following config variables to the values noted while configuring IDCS. The IDCS URL is the token endpoint that returns the access token after validating credentials

CLIENT_ID = "xxxxxxxxxxx";
CLIENT_SECRET = "xxxxxxxxx";
IDCS_URL = "https://idcs-xxxxxxxx.identity.oraclecloud.com/oauth2/v1/token";

//INFORMATION ABOUT THE TARGET APPLICATION
SCOPE_AUD = "display-httprequest-infodisplay-httprequest-info";

For the unit test to run, set the following variables in src/test/java/com/example/fn/BasicAuthTest.java

    private static final String TEST_IDCS_URL = "https://idcs-xxxxxxxx.identity.oraclecloud.com/oauth2/v1/token";
    private static final String TEST_CLIENT_ID = "xxxxxxxxxxx";
    private static final String TEST_CLIENT_SECRET = "xxxxxxxxxxx";
    private static final String TEST_SCOPE_AUD = "display-httprequest-infodisplay-httprequest-info";
    private static final String TEST_TOKEN = "xxxxxxxxxxx";

Deploy the basicauth function

In Cloud Shell, run fn deploy to build the function and its dependencies as a container, push the image to OCIR, and deploy the function to Oracle Functions in your application.

user input icon

fn -v deploy --app <app-name>

Invoke the basicauth function in cloud shell

In Cloud Shell, run fn invoke to invoke the deployed function. It should return an active status of true if the token is valid or otherwise returns false.

user input icon

echo -n '{"type":"TOKEN", "token":"Basic aW5jaGFyYS5zaGFtYW5uYUBvcmFj....."}' | fn invoke <app-name> <func-name>

Create the API Gateway

The functions is meant to be invoked through API Gateway.

user input icon

On the OCI console, navigate to Developer Services > API Gateway. Click on Create Gateway. Provide a name, set the type to "Public", select a compartment, a VCN, a public subnet, and click Create.

APIGW create

Once created, click on your gateway. Under Resources, select Deployments and click Create Deployment.

  • Provide a name, a path prefix ("/basicauth" for example).
  • Under API Request Policies Add Authentication
    • Authentication Type: Custom
    • Choose the application and the basicauth function
    • For "Authentication token", select Header
    • For the "Header Name", enter "Autorization"

Click Save Changes when you are finished APIGW deployment create

Click Next. Provide a name to the route ("/hello" for example), select methods eg: "GET", select HTTP-URL for your back-end.

APIGW deployment create

Click Next and finally, click Save Changes.

Note the endpoint of your API Gateway deployment.

APIGW deployment endpoint

Invoke the Deployment endpoint

The function validates if the user information is valid.

user input icon

Use curl to make the HTTP request

 curl -i -u "<username>:<password>" https://d6xxxxxxxxk64.apigateway.us-ashburn-1.oci.customer-oci.com/basicauth/hello

If the user is valid gateway will make a call to backend with HTTP200 else The gateway will reject the request with an HTTP401.