Skip to content

Commit

Permalink
feat: bring cognito (#117)
Browse files Browse the repository at this point in the history
* wip

* wip

* wip

* wip

* wip
  • Loading branch information
eladcon authored Mar 25, 2024
1 parent 7207227 commit 24ec853
Show file tree
Hide file tree
Showing 19 changed files with 2,625 additions and 5 deletions.
24 changes: 21 additions & 3 deletions .github/workflows/canary.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,27 @@ jobs:
- name: Test
run: wing test
working-directory: checks
canary-cognito:
name: Test 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
canary-dynamodb:
name: Test dynamodb
runs-on: ubuntu-latest
Expand Down Expand Up @@ -175,9 +196,6 @@ jobs:
- name: Test
run: wing test
working-directory: lock
- name: Test
run: wing test
working-directory: lock
canary-messagefanout:
name: Test messagefanout
runs-on: ubuntu-latest
Expand Down
29 changes: 29 additions & 0 deletions .github/workflows/cognito-pull.yaml
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
54 changes: 54 additions & 0 deletions .github/workflows/cognito-release.yaml
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 }}
2 changes: 1 addition & 1 deletion .github/workflows/messagefanout-pull.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: 18.x
node-version: 20.x
registry-url: https://registry.npmjs.org
- name: Install winglang
run: npm i -g winglang
Expand Down
3 changes: 2 additions & 1 deletion .github/workflows/messagefanout-release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ on:
- main
paths:
- messagefanout/**
- "!messagefanout/package-lock.json"
jobs:
build-messagefanout:
runs-on: ubuntu-latest
Expand All @@ -16,7 +17,7 @@ jobs:
- name: Setup Node.js
uses: actions/setup-node@v3
with:
node-version: 18.x
node-version: 20.x
registry-url: https://registry.npmjs.org
- name: Install winglang
run: npm i -g winglang
Expand Down
6 changes: 6 additions & 0 deletions .mergify.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ pull_request_rules:
- -check-failure=build-checks
- -check-pending=build-checks
- -check-stale=build-checks
- -check-failure=build-cognito
- -check-pending=build-cognito
- -check-stale=build-cognito
- -check-failure=build-containers
- -check-pending=build-containers
- -check-stale=build-containers
Expand Down Expand Up @@ -110,6 +113,9 @@ pull_request_rules:
- -check-failure=build-checks
- -check-pending=build-checks
- -check-stale=build-checks
- -check-failure=build-cognito
- -check-pending=build-cognito
- -check-stale=build-cognito
- -check-failure=build-containers
- -check-pending=build-containers
- -check-stale=build-containers
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@ publishing them for you.
| [bedrock](./bedrock) | [@winglibs/bedrock](https://www.npmjs.com/package/@winglibs/bedrock) | sim, tf-aws |
| [budget](./budget) | [@winglibs/budget](https://www.npmjs.com/package/@winglibs/budget) | sim, tf-aws |
| [checks](./checks) | [@winglibs/checks](https://www.npmjs.com/package/@winglibs/checks) | * |
| [cognito](./cognito) | [@winglibs/cognito](https://www.npmjs.com/package/@winglibs/cognito) | sim, tf-aws |
| [containers](./containers) | [@winglibs/containers](https://www.npmjs.com/package/@winglibs/containers) | sim, tf-aws |
| [dynamodb](./dynamodb) | [@winglibs/dynamodb](https://www.npmjs.com/package/@winglibs/dynamodb) | sim, tf-aws |
| [eventbridge](./eventbridge) | [@winglibs/eventbridge](https://www.npmjs.com/package/@winglibs/eventbridge) | awscdk, sim, tf-aws |
| [fifoqueue](./fifoqueue) | [@winglibs/fifoqueue](https://www.npmjs.com/package/@winglibs/fifoqueue) | sim, tf-aws |
| [github](./github) | [@winglibs/github](https://www.npmjs.com/package/@winglibs/github) | * |
| [lock](./lock) | [@winglibs/lock](https://www.npmjs.com/package/@winglibs/lock) | * |
| [messagefanout](./messagefanout) | [@winglibs/messagefanout](https://www.npmjs.com/package/@winglibs/messagefanout) | |
| [ngrok](./ngrok) | [@winglibs/ngrok](https://www.npmjs.com/package/@winglibs/ngrok) | * |
| [openai](./openai) | [@winglibs/openai](https://www.npmjs.com/package/@winglibs/openai) | * |
| [postgres](./postgres) | [@winglibs/postgres](https://www.npmjs.com/package/@winglibs/postgres) | sim, tf-aws |
Expand Down
2 changes: 2 additions & 0 deletions cognito/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
target/
node_modules/
21 changes: 21 additions & 0 deletions cognito/LICENSE
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.
76 changes: 76 additions & 0 deletions cognito/README.md
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).
56 changes: 56 additions & 0 deletions cognito/lib.test.w
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);
}
Loading

0 comments on commit 24ec853

Please sign in to comment.