-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* wip * wip * wip * wip * wip
- Loading branch information
Showing
19 changed files
with
2,625 additions
and
5 deletions.
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
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,29 @@ | ||
name: cognito-pull | ||
on: | ||
pull_request: | ||
paths: | ||
- cognito/** | ||
jobs: | ||
build-cognito: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v3 | ||
with: | ||
sparse-checkout: cognito | ||
- name: Setup Node.js | ||
uses: actions/setup-node@v3 | ||
with: | ||
node-version: 20.x | ||
registry-url: https://registry.npmjs.org | ||
- name: Install winglang | ||
run: npm i -g winglang | ||
- name: Install dependencies | ||
run: npm install --include=dev | ||
working-directory: cognito | ||
- name: Test | ||
run: wing test | ||
working-directory: cognito | ||
- name: Pack | ||
run: wing pack | ||
working-directory: cognito |
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,54 @@ | ||
name: cognito-release | ||
on: | ||
push: | ||
branches: | ||
- main | ||
paths: | ||
- cognito/** | ||
- "!cognito/package-lock.json" | ||
jobs: | ||
build-cognito: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v3 | ||
with: | ||
sparse-checkout: cognito | ||
- name: Setup Node.js | ||
uses: actions/setup-node@v3 | ||
with: | ||
node-version: 20.x | ||
registry-url: https://registry.npmjs.org | ||
- name: Install winglang | ||
run: npm i -g winglang | ||
- name: Install dependencies | ||
run: npm install --include=dev | ||
working-directory: cognito | ||
- name: Test | ||
run: wing test | ||
working-directory: cognito | ||
- name: Pack | ||
run: wing pack | ||
working-directory: cognito | ||
- name: Get package version | ||
run: echo WINGLIB_VERSION=$(node -p "require('./package.json').version") >> | ||
"$GITHUB_ENV" | ||
working-directory: cognito | ||
- name: Publish | ||
run: npm publish --access=public --registry https://registry.npmjs.org --tag | ||
latest *.tgz | ||
working-directory: cognito | ||
env: | ||
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }} | ||
- name: Tag commit | ||
uses: tvdias/[email protected] | ||
with: | ||
repo-token: ${{ secrets.PROJEN_GITHUB_TOKEN }} | ||
tag: cognito-v${{ env.WINGLIB_VERSION }} | ||
- name: GitHub release | ||
uses: softprops/action-gh-release@v1 | ||
with: | ||
name: cognito v${{ env.WINGLIB_VERSION }} | ||
tag_name: cognito-v${{ env.WINGLIB_VERSION }} | ||
files: "*.tgz" | ||
token: ${{ secrets.PROJEN_GITHUB_TOKEN }} |
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
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
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
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
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 @@ | ||
target/ | ||
node_modules/ |
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,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2023 Wing | ||
|
||
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, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
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. |
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,76 @@ | ||
# cognito | ||
|
||
A wing library to work with AWS Cognito. | ||
|
||
## Prerequisites | ||
|
||
* [winglang](https://winglang.io). | ||
|
||
## Installation | ||
|
||
`sh | ||
npm i @winglibs/cognito | ||
` | ||
|
||
## Usage | ||
|
||
```js | ||
bring cognito; | ||
|
||
let api = new cloud.Api(); | ||
api.get("/hello", inflight (req) => { | ||
return { | ||
status: 200 | ||
}; | ||
}); | ||
|
||
let auth = new cognito.Cognito(api); | ||
auth.get("/hello"); | ||
``` | ||
|
||
## Test | ||
|
||
### Wing Code | ||
|
||
```js | ||
auth.signUp("[email protected]", "This-is-my-test-99!"); | ||
auth.adminConfirmUser("[email protected]"); | ||
let token = auth.initiateAuth("[email protected]", "This-is-my-test-99!"); | ||
let res = http.get("{api.url}/hello", headers: { | ||
"Authorization": "Bearer {token}" | ||
}); | ||
expect.equal(res.status, 200); | ||
``` | ||
|
||
### AWS CLI | ||
|
||
Create a user | ||
|
||
```sh | ||
aws cognito-idp sign-up \ | ||
--client-id ${USER_POOL_CLIENT_ID} \ | ||
--username [email protected] \ | ||
--password NicePassw0rd! \ | ||
--user-attributes Name=name,Value=ekeren Name=email,[email protected] | ||
``` | ||
|
||
Get a token for the user (make sure user is confirmed) | ||
|
||
```sh | ||
aws cognito-idp initiate-auth \ | ||
--client-id ${USER_POOL_CLIENT_ID} \ | ||
--auth-flow USER_PASSWORD_AUTH \ | ||
--auth-parameters [email protected],PASSWORD=NicePassw0rd! \ | ||
--query 'AuthenticationResult.IdToken' \ | ||
--output text | ||
``` | ||
|
||
Send a request | ||
|
||
```sh | ||
curl -H "Authorization: ${TOKEN}" https://5b0y949eik.execute-api.us-east-1.amazonaws.com/prod/hello | ||
``` | ||
|
||
## License | ||
|
||
This library is licensed under the [MIT License](./LICENSE). |
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,56 @@ | ||
bring cloud; | ||
bring expect; | ||
bring http; | ||
bring util; | ||
bring aws; | ||
bring "./lib.w" as l; | ||
|
||
let api = new cloud.Api(); | ||
api.get("/hello", inflight (req) => { | ||
log("GET - This route is protected"); | ||
|
||
return { | ||
status: 200 | ||
}; | ||
}); | ||
|
||
api.post("/hello", inflight (req) => { | ||
log("POST - This route is protected too"); | ||
|
||
return { | ||
status: 200 | ||
}; | ||
}); | ||
|
||
api.put("/hello", inflight (req) => { | ||
log("PUT - This route is not protected"); | ||
|
||
return { | ||
status: 200 | ||
}; | ||
}); | ||
|
||
let auth = new l.Cognito(api); | ||
auth.get("/hello"); | ||
auth.post("/hello"); | ||
|
||
test "access endpoint with cognito auth" { | ||
expect.equal(http.get("{api.url}/hello").status, 401); | ||
expect.equal(http.post("{api.url}/hello").status, 401); | ||
expect.equal(http.put("{api.url}/hello").status, 200); | ||
|
||
auth.signUp("[email protected]", "This-is-my-test-99!"); | ||
auth.adminConfirmUser("[email protected]"); | ||
let token = auth.initiateAuth("[email protected]", "This-is-my-test-99!"); | ||
let res2 = http.get("{api.url}/hello", headers: { | ||
"Authorization": "Bearer {token}" | ||
}); | ||
|
||
expect.equal(res2.status, 200); | ||
|
||
let res3 = http.post("{api.url}/hello", headers: { | ||
"Authorization": "Bearer {token}" | ||
}); | ||
|
||
expect.equal(res3.status, 200); | ||
} |
Oops, something went wrong.