Giter VIP home page Giter VIP logo

kong-oauth2's Introduction

OAuth 2.0 Authentication

Add an OAuth 2.0 authentication layer with the Resource Owner Password Credentials Grant flow.

Create a Service

curl -i -X POST \
  --url http://localhost:8001/services/ \
  --data 'name=google-svc' \
  --data 'url=http://google.com'

We have service_id.

Create a Route

Replace service_id in /services/{service_id}/routes

curl -i -X POST \
  --url http://localhost:8001/services/google-svc/routes \
  --data 'hosts[]=localhost' \
  --data 'methods[]=GET&methods[]=POST'

We get route_id.

Test

curl -i -X GET \
  --url http://localhost:8000/

We will see Google website content

Create a Consumer

curl -X POST http://localhost:8001/consumers/ \
    --data "username=phong" \
    --data "custom_id=phong_id"

We get consumer_id.

Create an Application

Replace consumer_id in /consumers/{consumer_id}/oauth2.

curl -X POST http://localhost:8001/consumers/01b28940-7ece-4179-8b58-73b731d8e607/oauth2 \
    --data "name=oauth-2-app" \
    --data "client_id=cid" \
    --data "client_secret=cserect" \
    --data "redirect_uri=http://google.com"

We have client_id and client_secret.

Enabling the plugin on a Service

Replace service_id in /services/{service_id}/plugins.

curl -X POST http://localhost:8001/services/google-svc/plugins \
    --data "name=oauth2"  \
    --data "config.scopes=email,read" \
    --data "config.mandatory_scope=true" \
    --data "config.enable_password_grant=true"

We get provision_key.

Get token

Parameters:

  • authenticated_userid = custom_id
curl -k https://localhost:8443/google-svc/oauth2/token \
     --data "client_id=cid" \
     --data "client_secret=cserect" \
     --data "grant_type=password" \
     --data "scope=read" \
     --data "provision_key=kDNBwCVrTjQU5cLQI2FDyI6onAYpAIBI" \
     --data "authenticated_userid=phong_id"

We get access_token and refesh_token.

Test without oauth2

curl -i -X GET \
  --url http://localhost:8000/

Error

{
    "error_description": "The access token is missing",
    "error": "invalid_request"
}

Test with oauth2

Replace access_token in Authorization: Bearer {access_token}.

curl -i -X GET \
  --url http://localhost:8000/ \
  --header "Authorization: Bearer a503faf9-45b5-4fec-8334-337284a66ea4"

We will see Google website content again.

Refresh token

Replace refresh_token in --data "refresh_token={refresh_token}".

curl -k POST https://localhost:8443/google-svc/oauth2/token \
    --data "grant_type=refresh_token" \
    --data "client_id=ci" \
    --data "client_secret=csecret" \
    --data "refresh_token=a503faf9-45b5-4fec-8334-337284a66ea4"

We get new access_token and refesh_token.

kong-oauth2's People

Contributors

phongca22 avatar

Stargazers

李炎龙 avatar

Watchers

James Cloos 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.