GithubHelp home page GithubHelp logo

tribemedia / kong-oauth2-hello-world Goto Github PK

View Code? Open in Web Editor NEW

This project forked from kong/kong-oauth2-hello-world

0.0 3.0 0.0 128 KB

This is a simple node.js + express.js application that shows an authorization page for the OAuth 2.0 plugin on Kong.

JavaScript 83.82% HTML 16.18%

kong-oauth2-hello-world's Introduction

OAuth 2.0 Hello World for Kong

This is a simple node.js + express.js + jade application that demonstrates a simple implementation of the OAuth 2.0 authorization page required to make the OAuth 2.0 plugin work on Kong.

Files

This project is made of two main files:

  • app.js, which handles the server and contains two routes:
    • GET /authorize, that shows the authorization page to the end user
    • POST /authorize, that handles the form submit and triggers the authorization process on Kong
  • authorization.jade, which is the authorization page that the user will see

Installing dependencies

Execute

npm install

Setting up the environment

To run this project, execute the following operations.

  • Make sure you have Kong >= 0.4.0 running. We assume Kong is running at 127.0.0.1 with the default ports.

  • Let's add a simple test API:

curl -d "public_dns=test.com" \
     -d "target_url=http://mockbin.org/" \
     http://127.0.0.1:8001/apis/
  • Let's add the OAuth 2.0 plugin, with three available scopes:
curl -d "name=oauth2" \
     -d "value.scopes=email, phone, address" \
     -d "value.mandatory_scope=true" \
     http://127.0.0.1:8001/apis/test.com/plugins/

This will output a response including an auto-generated provision_key that we need to use later:

{
    "api_id": "2c0c8c84-cd7c-40b7-c0b8-41202e5ee50b",
    "value": {
        "scopes": [
            "email",
            "phone",
            "address"
        ],
        "mandatory_scope": true,
        "provision_key": "2ef290c575cc46eec61947aa9f1e67d3",
        "hide_credentials": false,
        "enable_implicit_grant": false,
        "token_expiration": 7200
    },
    "created_at": 1435783325000,
    "enabled": true,
    "name": "oauth2",
    "id": "656954bd-2130-428f-c25c-8ec47227dafa"
}

The provision_key will be sent by the web application when communicating with Kong, to securely authenticate itself with Kong.

  • Let's create a Kong consumer (called thefosk):
curl -d "username=thefosk" \
     http://127.0.0.1:8001/consumers/
  • And the first OAuth 2.0 client application called Hello World App:
curl -d "name=Hello World App" \
     -d "redirect_uri=http://getkong.org/" \
     http://127.0.0.1:8001/consumers/thefosk/oauth2/

That outputs the following response, including the client_id and client_secret that we will use later:

{
    "consumer_id": "a0977612-bd8c-4c6f-ccea-24743112847f",
    "client_id": "318f98be1453427bc2937fceab9811bd",
    "id": "7ce2f90c-3ec5-4d93-cd62-3d42eb6f9b64",
    "name": "Hello World App",
    "created_at": 1435783376000,
    "redirect_uri": "http://getkong.org/",
    "client_secret": "efbc9e1f2bcc4968c988ef5b839dd5a4"
}

Running the web application

Now that Kong has all the data configured, we can start our application using the provision_key that has been returned when we added the plugin:

# Exporting some environment variables used by the Node.js application
export PROVISION_KEY="2ef290c575cc46eec61947aa9f1e67d3"
export KONG_ADMIN="http://127.0.0.1:8001"
export KONG_API="http://127.0.0.1:8000"
export API_PUBLIC_DNS="test.com"
export SCOPES="{ \
  \"email\": \"Grant permissions to read your email address\", \
  \"address\": \"Grant permissions to read your address information\", \
  \"phone\": \"Grant permissions to read your mobile phone number\" \
}"

# Starting the node.js application
node app.js

Testing the Authorization Flow

To start the authorization flow we need to simulate the request that the client application will execute when redirecting the user to your API. This request will include the response_type parameter, the client_id and the scope requested.

Note: In our example we are skipping the log-in of the user, which is something you will do in production before showing the authorization page.

With your browser, go to http://127.0.0.1:3000/authorize?response_type=code&scope=email, address&client_id=318f98be1453427bc2937fceab9811bd to show the authrorization page. You will see a page like:

Authorization Prompt

After clicking the "Authorize" button, you should be redirected to the redirect_uri we set up before with a code parameter in the querystring, like:

http://getkong.org/?code=ad286cf6694d40aac06eff2797b7208d

For testing purposes we set the redirect_uri to http://getkong.org, but in production this will be an URL that the client application will be able to read to parse the code and exchange it with an access token.

Conclusions

Done! Now the client application has a code that it can use later on to request an access_token. From a provider perspective our job only consists in showing the authorization page and redirecting the user.

kong-oauth2-hello-world's People

Contributors

subnetmarco avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.