-
Notifications
You must be signed in to change notification settings - Fork 38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Typer CLI implementation with cognito device authorization flow #93
Open
suhussai
wants to merge
26
commits into
main
Choose a base branch
from
command-line-interface
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 25 commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
fd32048
feat: Typer CLI with device authorization grant
ar-aws 3a051d8
Update publish-to-test-pypi.yml
ar-aws a1b3acb
Update main.py
ar-aws 9096e32
Update README.md
ar-aws 1078ca8
Update publish-to-test-pypi.yml
ar-aws b555afd
Update cli-auth.ts
ar-aws 824fafb
Update cli-auth.ts
ar-aws 32a4bef
Merge branch 'awslabs:main' into command-line-interface
ar-aws 2a959cf
configured resources
ar-aws 35f3f7a
feat: Typer CLI with device authorization grant
ar-aws f0b4df5
Update publish-to-test-pypi.yml
ar-aws 066c3e7
Update main.py
ar-aws adae9c7
Update README.md
ar-aws dba6c16
Update publish-to-test-pypi.yml
ar-aws f6756bd
Update cli-auth.ts
ar-aws eb84259
Update cli-auth.ts
ar-aws 4fd5084
configured resources
ar-aws b622b6d
Update README.md
ar-aws 3d352f3
merge
ar-aws 0d7e978
Update README.md
ar-aws e37b7f8
Update README.md
ar-aws fc34f83
Update API.md
ar-aws ddc009b
Merge branch 'awslabs:main' into command-line-interface
ar-aws 6434a70
fix formatting on cli-auth.ts file
suhussai 240438c
update cli auth construct for cognito auth
suhussai 52480c0
Reduced number of CLI props
beautse File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
name: Publish Python 🐍 distribution 📦 to PyPI | ||
|
||
on: | ||
push: | ||
paths: | ||
- 'scripts/sbt-aws-cli/pyproject.toml' | ||
|
||
jobs: | ||
build: | ||
name: Build distribution 📦 | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- uses: actions/checkout@v4 | ||
|
||
- name: Set up Python | ||
uses: actions/setup-python@v5 | ||
with: | ||
python-version: "3.x" | ||
|
||
- name: Install Poetry | ||
run: pip install poetry | ||
|
||
- name: Install dependencies | ||
run: poetry install --no-dev | ||
working-directory: scripts/sbt-aws-cli | ||
|
||
- name: Build the distribution | ||
run: poetry build | ||
working-directory: scripts/sbt-aws-cli | ||
|
||
- name: List files in dist/ | ||
run: ls -la scripts/sbt-aws-cli/dist/ | ||
|
||
- name: Store the distribution packages | ||
uses: actions/upload-artifact@v3 | ||
with: | ||
name: python-package-distributions | ||
path: scripts/sbt-aws-cli/dist/ | ||
|
||
publish-to-pypi: | ||
name: Publish Python 🐍 distribution 📦 to PyPI | ||
needs: build | ||
runs-on: ubuntu-latest | ||
environment: | ||
name: pypi | ||
url: https://pypi.org/project/sbt-aws-cli | ||
permissions: | ||
id-token: write | ||
|
||
steps: | ||
- name: Download all the dists | ||
uses: actions/download-artifact@v3 | ||
with: | ||
name: python-package-distributions | ||
path: scripts/sbt-aws-cli/dist/ | ||
|
||
- name: List files in dist/ after download | ||
run: ls -la scripts/sbt-aws-cli/dist/ | ||
|
||
- name: Publish distribution 📦 to PyPI | ||
uses: pypa/gh-action-pypi-publish@release/v1 | ||
with: | ||
packages-dir: scripts/sbt-aws-cli/dist/ | ||
repository-url: https://upload.pypi.org/legacy/ | ||
skip-existing: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[tools] | ||
python = {version='3.9', virtualenv='.venv'} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
resources/functions/auth-device-grant-token/Modules/authorization-path.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: MIT-0 | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy of this | ||
* software and associated documentation files (the "Software"), to deal in the Software | ||
* without restriction, including without limitation the rights to use, copy, modify, | ||
* merge, publish, distribute, sublicense, and/or sell copies of the Software, and to | ||
* permit persons to whom the Software is furnished to do so. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, | ||
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A | ||
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT | ||
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | ||
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE | ||
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
*/ | ||
//Reference to Commnon library | ||
const common = require( __dirname + '/common.js'); | ||
|
||
const { CognitoIdentityProvider } = require("@aws-sdk/client-cognito-identity-provider"); | ||
const { DynamoDB } = require("@aws-sdk/client-dynamodb"); | ||
|
||
//Reference to Crypto library for PKCE challenge | ||
const crypto = require('crypto'); | ||
|
||
var cognitoidentityserviceprovider = new CognitoIdentityProvider({ | ||
// The transformation for apiVersions is not implemented. | ||
// Refer to UPGRADING.md on aws-sdk-js-v3 for changes needed. | ||
// Please create/upvote feature request on aws-sdk-js-codemod for apiVersions. | ||
// The transformation for apiVersions is not implemented. | ||
// Refer to UPGRADING.md on aws-sdk-js-v3 for changes needed. | ||
// Please create/upvote feature request on aws-sdk-js-codemod for apiVersions. | ||
apiVersions: { | ||
cognitoidentityserviceprovider: '2016-04-18', | ||
}, | ||
}); | ||
var dynamodb = new DynamoDB({ | ||
// The transformation for apiVersions is not implemented. | ||
// Refer to UPGRADING.md on aws-sdk-js-v3 for changes needed. | ||
// Please create/upvote feature request on aws-sdk-js-codemod for apiVersions. | ||
// The transformation for apiVersions is not implemented. | ||
// Refer to UPGRADING.md on aws-sdk-js-v3 for changes needed. | ||
// Please create/upvote feature request on aws-sdk-js-codemod for apiVersions. | ||
apiVersions: { | ||
dynamodb: '2012-08-10', | ||
}, | ||
}); | ||
|
||
//Function that processes "Authorize" by an authenticated end user for a valid user code | ||
// client_id: Client ID of the client application that initiated the Authorization request | ||
// device_code: Primary key of the "Authorized" Authorization request in the DynamoDB table | ||
// callback: Callback function to return the message | ||
// dynamodb: Pointer to the DynamoDB SDK request handler | ||
function processAllow(client_id, device_code, callback) { | ||
|
||
//Generating a code verifier and challenge for the PKCE protection of the OAuth2 flow | ||
var code_verifier = common.randomString(32, 'aA#'); | ||
var hash = crypto.createHash('sha256').update(code_verifier).digest(); | ||
var code_challenge = common.base6UurlEncode(hash); | ||
|
||
//Generating a random state for preventing against CSRF attacks | ||
var state = common.randomString(32, 'aA#'); | ||
|
||
//Updating the Authorization request with PKCE code verifier and State | ||
var DynamoDBParams = { | ||
ExpressionAttributeNames: { | ||
"#AuthZ_State": "AuthZ_State", | ||
"#AuthZ_Verif": "AuthZ_Verifier_code", | ||
}, | ||
ExpressionAttributeValues: { | ||
":authz_state": { | ||
S: state | ||
}, | ||
":authz_verif": { | ||
S: code_verifier | ||
} | ||
}, | ||
Key: { | ||
"Device_code": { | ||
S: device_code | ||
} | ||
}, | ||
ReturnValues: "ALL_NEW", | ||
TableName: process.env.DYNAMODB_TABLE, | ||
UpdateExpression: "SET #AuthZ_State = :authz_state, #AuthZ_Verif = :authz_verif" | ||
}; | ||
dynamodb.updateItem(DynamoDBParams, function(err, data) { | ||
if (err) { | ||
//There was an error updating the Authorization request | ||
console.log("Unable to set Authorization State and Verifier Code for Device Code = " + device_code); | ||
common.returnHTMLError(400, "<H1>Error, can't update status</H1>", callback); | ||
} | ||
else { | ||
//Update was successful so triggering a standard Authorization Code Grant flow with PKCE to Cognito using the inial Client Application's Client ID | ||
var response = { | ||
statusCode: 302, | ||
headers: {"location": "https://" + process.env.CUP_DOMAIN + ".auth." + process.env.CUP_REGION + ".amazoncognito.com/oauth2/authorize?response_type=code&client_id=" + client_id + "&redirect_uri=" + encodeURIComponent("https://" + process.env.CODE_VERIFICATION_URI + "/callback") + "&state=" + state + "&scope=" + data.Attributes.Scope.S + "&code_challenge_method=S256&code_challenge=" + code_challenge + "&identity_provider=COGNITO"}, | ||
}; | ||
callback(null, response); | ||
} | ||
}); | ||
} | ||
|
||
module.exports = Object.assign({ processAllow }); |
119 changes: 119 additions & 0 deletions
119
resources/functions/auth-device-grant-token/Modules/callback-path.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
/* | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: MIT-0 | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy of this | ||
* software and associated documentation files (the "Software"), to deal in the Software | ||
* without restriction, including without limitation the rights to use, copy, modify, | ||
* merge, publish, distribute, sublicense, and/or sell copies of the Software, and to | ||
* permit persons to whom the Software is furnished to do so. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, | ||
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A | ||
* PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT | ||
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION | ||
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE | ||
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. | ||
*/ | ||
//Reference to Commnon library | ||
const common = require( __dirname + '/common.js'); | ||
|
||
const { CognitoIdentityProvider } = require("@aws-sdk/client-cognito-identity-provider"); | ||
const { DynamoDB } = require("@aws-sdk/client-dynamodb"); | ||
|
||
var cognitoidentityserviceprovider = new CognitoIdentityProvider({ | ||
// The transformation for apiVersions is not implemented. | ||
// Refer to UPGRADING.md on aws-sdk-js-v3 for changes needed. | ||
// Please create/upvote feature request on aws-sdk-js-codemod for apiVersions. | ||
// The transformation for apiVersions is not implemented. | ||
// Refer to UPGRADING.md on aws-sdk-js-v3 for changes needed. | ||
// Please create/upvote feature request on aws-sdk-js-codemod for apiVersions. | ||
apiVersions: { | ||
cognitoidentityserviceprovider: '2016-04-18', | ||
}, | ||
}); | ||
var dynamodb = new DynamoDB({ | ||
// The transformation for apiVersions is not implemented. | ||
// Refer to UPGRADING.md on aws-sdk-js-v3 for changes needed. | ||
// Please create/upvote feature request on aws-sdk-js-codemod for apiVersions. | ||
// The transformation for apiVersions is not implemented. | ||
// Refer to UPGRADING.md on aws-sdk-js-v3 for changes needed. | ||
// Please create/upvote feature request on aws-sdk-js-codemod for apiVersions. | ||
apiVersions: { | ||
dynamodb: '2012-08-10', | ||
}, | ||
}); | ||
|
||
//Function that processes Cognito callback after end user Authorization Code grant flow with PKCE request | ||
// event: Full event trapped by the Lambda function | ||
// callback: Callback function to return the message | ||
function processAuthZCodeCallback(event, callback) { | ||
console.log("An Authorization Code has been sent back as Callback"); | ||
|
||
//Search the DynamoDb table for Authorization Request with provided State | ||
var DynamoDBParams = { | ||
ExpressionAttributeValues: { | ||
":authz_state": { | ||
S: event.queryStringParameters.state | ||
} | ||
}, | ||
KeyConditionExpression: "AuthZ_State = :authz_state", | ||
IndexName: process.env.DYNAMODB_AUTHZ_STATE_INDEX, | ||
TableName: process.env.DYNAMODB_TABLE | ||
}; | ||
dynamodb.query(DynamoDBParams, function(err, data) { | ||
if (err) { | ||
//There was an error retrieving the Authorization request | ||
console.log("Authorization State can't be retrieved: " + event.queryStringParameters.state); | ||
console.log(err, err.stack); | ||
common.returnHTMLError(400, "<H1>Error, can't update status</H1>", callback); | ||
} else { | ||
console.log("Successful response"); | ||
//If there is no result set | ||
if (data.Items.length == 0) { | ||
console.log("No AuthZ State was returned"); | ||
common.returnHTMLError(400, "<H1>Error, can't update status</H1>", callback); | ||
//If Result Set is more than 1 entry | ||
} else if (data.Items.length > 1) { | ||
console.log("Too much AuthZ State were returned"); | ||
common.returnHTMLError(400, "<H1>Error, can't update status</H1>", callback); | ||
} else { | ||
console.log("AuthZ State was returned"); | ||
// Updating the Authorization request with the Code returned through the Authorization Code grant flow with PKCE callback | ||
DynamoDBParams = { | ||
ExpressionAttributeNames: { | ||
"#AuthZ_code": "AuthZ_code" | ||
}, | ||
ExpressionAttributeValues: { | ||
":value": { | ||
S: event.queryStringParameters.code | ||
} | ||
}, | ||
Key: { | ||
"Device_code": { | ||
S: data.Items[0].Device_code.S | ||
} | ||
}, | ||
ReturnValues: "ALL_NEW", | ||
TableName: process.env.DYNAMODB_TABLE, | ||
UpdateExpression: "SET #AuthZ_code = :value" | ||
}; | ||
dynamodb.updateItem(DynamoDBParams, function(err, data) { | ||
if (err) { | ||
//Update was not successful | ||
console.log("Unable to set state to Authorization Code for Device Code"); | ||
console.log(err, err.stack); | ||
common.returnHTMLError(400, "<H1>Error, can't update status</H1>", callback); | ||
} | ||
else { | ||
//Update was successful | ||
console.log("AuthZ Code updated"); | ||
common.returnHTMLSuccess("<H1>Thanks, Device has been Authorized. You can return to your device.</H1>", callback); | ||
} | ||
}); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
module.exports = Object.assign({ processAuthZCodeCallback }); |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does the actual executable have to be called
sbt-aws-cli
? Couldn't we make it justsbt-aws
or evensbt
?