From 843e15efefbe7cb33bdc6ae5ab32a24cc7d581cd Mon Sep 17 00:00:00 2001 From: t-burch Date: Mon, 30 Oct 2023 11:33:13 +0100 Subject: [PATCH] Removed shell scripts and replaced them with a postman configuration and updated readme instructions. --- distribution/examples/oauth2/api/README.md | 19 +++- distribution/examples/oauth2/api/client.bat | 1 - distribution/examples/oauth2/api/client.ps1 | 38 ------- distribution/examples/oauth2/api/client.sh | 36 ------- .../examples/oauth2/api/environment.json | 15 +++ .../examples/oauth2/api/requests.json | 101 ++++++++++++++++++ 6 files changed, 134 insertions(+), 76 deletions(-) delete mode 100644 distribution/examples/oauth2/api/client.bat delete mode 100644 distribution/examples/oauth2/api/client.ps1 delete mode 100755 distribution/examples/oauth2/api/client.sh create mode 100644 distribution/examples/oauth2/api/environment.json create mode 100644 distribution/examples/oauth2/api/requests.json diff --git a/distribution/examples/oauth2/api/README.md b/distribution/examples/oauth2/api/README.md index c23ffb641c..5573438eec 100644 --- a/distribution/examples/oauth2/api/README.md +++ b/distribution/examples/oauth2/api/README.md @@ -2,4 +2,21 @@ This example shows the OAuth2 password flow. A client requests an access token ( with user credentials ) and the token is verified through a token validator. -Please follow https://www.membrane-soa.org/api-gateway-doc/current/oauth2-password-flow-sample.html \ No newline at end of file + +## Running the example + +### Setup + +1. Go to the [Postman](https://www.postman.com/downloads/) website and install the latest desktop client for your OS. +2. Open the Postman app and drag `environment.json` and `requests.json` into the menu bar to the right. +3. Click on the `environments` tab and tick the checkmark on the `oauth2 environment`. +4. Click on the `collections` tab and open the `oauth2 example requests` folder. + +### Sending the Requests + +1. Click on the `POST` request and press `Send`. This will request an oauth token from the authorization server using the username `john` and password `password`. +2. Click on the `GET` request and send it. Now the previously acquired oauth token will be set in the `Authorization` header using the `Bearer` keyword to authenticate with the token validator. + +See: +- [oauth2authserver](https://www.membrane-soa.org/api-gateway-doc/5.2/configuration/reference/oauth2authserver.htm) reference +- [tokenValidator](https://www.membrane-soa.org/api-gateway-doc/5.2/configuration/reference/tokenValidator.htm) reference \ No newline at end of file diff --git a/distribution/examples/oauth2/api/client.bat b/distribution/examples/oauth2/api/client.bat deleted file mode 100644 index f7d5bd9307..0000000000 --- a/distribution/examples/oauth2/api/client.bat +++ /dev/null @@ -1 +0,0 @@ -powershell client.ps1 \ No newline at end of file diff --git a/distribution/examples/oauth2/api/client.ps1 b/distribution/examples/oauth2/api/client.ps1 deleted file mode 100644 index b288429c2d..0000000000 --- a/distribution/examples/oauth2/api/client.ps1 +++ /dev/null @@ -1,38 +0,0 @@ -param ( - [Parameter(Mandatory=$true)] - [ValidateNotNullOrEmpty()] - [string]$username, - [Parameter(Mandatory=$true)] - [ValidateNotNullOrEmpty()] - [string]$password - ) - -$clientId = "abc" -$clientSecret = "def" -$tokenEndpoint = "http://localhost:7007/oauth2/token" -$target = "http://localhost:2000" - -function getToken{ - Write-Host "1.) Requesting Token" - Write-Host "POST $tokenEndpoint" - $postParams = @{grant_type="password";username=$username;password=$password;client_id=$clientId;client_secret=$clientSecret} - Write-Host $postParams - Write-Host - return Invoke-WebRequest -Uri $tokenEndpoint -Method POST -Body $postParams | ConvertFrom-Json -} - -function sendRequestToTarget($tokenResult){ - Write-Host - Write-Host "2.) Calling API" - Write-Host "GET $target" - $headers = @{"Authorization"=$tokenResult.token_type + " " + $tokenResult.access_token} - Write-Host Authorization: $headers["Authorization"] - Write-Host - return Invoke-WebRequest -Uri $target -Headers $headers -} - -$tokenEndpointResult = getToken -Write-Host "Got Token:" $tokenEndpointResult.access_token -Write-Host -$result = sendRequestToTarget $tokenEndpointResult -Write-Host "Got": $result.Content \ No newline at end of file diff --git a/distribution/examples/oauth2/api/client.sh b/distribution/examples/oauth2/api/client.sh deleted file mode 100755 index 4c39f968f2..0000000000 --- a/distribution/examples/oauth2/api/client.sh +++ /dev/null @@ -1,36 +0,0 @@ -clientId="abc" -clientSecret="def" -tokenEndpoint="http://localhost:7007/oauth2/token" -target="http://localhost:2000" - -username=$1 -password=$2 - -parseResponse(){ - IFS='"' read -ra ADDR <<< "$1" - echo "Got Token: ${ADDR[3]}" - authHeader="Authorization: ${ADDR[7]} ${ADDR[3]}" -} - -getToken(){ - body="grant_type=password&username=${username}&password=${password}&client_id=${clientId}&client_secret=${clientSecret}" - echo "1.) Requesting Token" - echo "POST $tokenEndpoint" - echo $body - echo - call=$(curl -s -d $body $tokenEndpoint) - parseResponse $call -} - -sendRequestToTarget(){ - echo - echo "2.) Calling API" - echo "GET $target" - echo "$authHeader" - targetResult=$(curl -s -H "$authHeader" $target) - echo - echo Got: $targetResult -} - -getToken -sendRequestToTarget \ No newline at end of file diff --git a/distribution/examples/oauth2/api/environment.json b/distribution/examples/oauth2/api/environment.json new file mode 100644 index 0000000000..bb92e6469d --- /dev/null +++ b/distribution/examples/oauth2/api/environment.json @@ -0,0 +1,15 @@ +{ + "id": "c36d2677-2ff1-4e07-be96-54919137ef0f", + "name": "oauth2 environment", + "values": [ + { + "key": "accessToken", + "value": "", + "type": "secret", + "enabled": true + } + ], + "_postman_variable_scope": "environment", + "_postman_exported_at": "2023-10-30T10:00:15.840Z", + "_postman_exported_using": "Postman/10.19.10" +} \ No newline at end of file diff --git a/distribution/examples/oauth2/api/requests.json b/distribution/examples/oauth2/api/requests.json new file mode 100644 index 0000000000..bd40ed9edb --- /dev/null +++ b/distribution/examples/oauth2/api/requests.json @@ -0,0 +1,101 @@ +{ + "info": { + "_postman_id": "27c65ce6-2e99-49ee-8f2c-8b501b476058", + "name": "oauth2 example requests", + "schema": "https://schema.getpostman.com/json/collection/v2.1.0/collection.json", + "_exporter_id": "30827720" + }, + "item": [ + { + "name": "http://localhost:7007/oauth2/token", + "event": [ + { + "listen": "test", + "script": { + "exec": [ + "var jsonData = pm.response.json();\r", + "pm.environment.set(\"accessToken\", jsonData.access_token);\r", + "pm.environment.set(\"tokenType\", jsonData.token_type);" + ], + "type": "text/javascript" + } + } + ], + "request": { + "method": "POST", + "header": [], + "body": { + "mode": "urlencoded", + "urlencoded": [ + { + "key": "grant_type", + "value": "password", + "type": "text" + }, + { + "key": "username", + "value": "john", + "type": "text" + }, + { + "key": "password", + "value": "password", + "type": "text" + }, + { + "key": "client_id", + "value": "abc", + "type": "text" + }, + { + "key": "client_secret", + "value": "def", + "type": "text" + } + ] + }, + "url": { + "raw": "http://localhost:7007/oauth2/token", + "protocol": "http", + "host": [ + "localhost" + ], + "port": "7007", + "path": [ + "oauth2", + "token" + ] + }, + "description": "1.) Requesting Token" + }, + "response": [] + }, + { + "name": "http://localhost:2000", + "request": { + "auth": { + "type": "bearer", + "bearer": [ + { + "key": "token", + "value": "{{accessToken}}", + "type": "string" + } + ] + }, + "method": "GET", + "header": [], + "url": { + "raw": "http://localhost:2000", + "protocol": "http", + "host": [ + "localhost" + ], + "port": "2000" + }, + "description": "2.) Calling API" + }, + "response": [] + } + ] +} \ No newline at end of file