Skip to content

Commit 0a2d642

Browse files
committed
Add eslint
1 parent 839814f commit 0a2d642

File tree

10 files changed

+1480
-190
lines changed

10 files changed

+1480
-190
lines changed

.eslintrc

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
{
2+
"root": true,
3+
"env": {
4+
"browser": true,
5+
"node": true,
6+
"es2021": true
7+
},
8+
"parserOptions": {
9+
"ecmaVersion": "latest",
10+
"sourceType": "module"
11+
},
12+
"extends": [
13+
"airbnb-base",
14+
"plugin:import/typescript"
15+
],
16+
"parser": "@typescript-eslint/parser",
17+
"plugins": [
18+
"@typescript-eslint"
19+
],
20+
"rules": {
21+
"@typescript-eslint/indent": [
22+
"error",
23+
2
24+
],
25+
"spaced-comment": [
26+
"error",
27+
"always",
28+
{ "markers": ["/ <reference"] }
29+
],
30+
"import/no-extraneous-dependencies": ["off", {
31+
"devDependencies": true,
32+
"optionalDependencies": false
33+
}],
34+
"max-len": ["error", {"code": 200}],
35+
"@next/next/no-img-element": ["off"],
36+
"prefer-promise-reject-errors": ["off"],
37+
"react/jsx-filename-extension": ["off"],
38+
"react/prop-types": ["off"],
39+
"import/extensions": ["off"],
40+
"jsx-a11y/anchor-is-valid": ["off"],
41+
"no-return-assign": ["off"],
42+
"react/display-name": ["off"],
43+
"import/prefer-default-export": ["off"],
44+
"camelcase": ["off"],
45+
"no-unused-vars": ["off"]
46+
}
47+
}

cdk.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
2-
"app": "rm -rf *.pem && yarn run -p create-keypair --bits 4096 jwtRS256 && npx ts-node --prefer-ts-exts bin/github-cognito-oidc-proxy.ts",
2+
"app": "test -e jwtRS256.private.pem || yarn run -p create-keypair --bits 4096 jwtRS256 && npx ts-node --prefer-ts-exts bin/github-cognito-oidc-proxy.ts",
33
"watch": {
44
"include": [
55
"**"

lambda/authorize/index.ts

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,30 @@
1-
import { Handler, APIGatewayProxyEventV2, APIGatewayProxyResultV2 } from 'aws-lambda';
2-
3-
export const handler: Handler<APIGatewayProxyEventV2, APIGatewayProxyResultV2> = async (event, _context, _callback) => {
4-
const { client_id, scope, state, response_type } = event.queryStringParameters || {};
5-
const redirectUri = `https://github.com?client_id=${client_id}&scope=${encodeURIComponent(scope!)}&state=${state}&response_type=${response_type}`;
6-
7-
return {
8-
cookies: [],
9-
statusCode: 200,
10-
headers: { Location: redirectUri },
11-
};
12-
}
1+
import {
2+
Handler,
3+
APIGatewayProxyEventV2,
4+
APIGatewayProxyResultV2,
5+
} from 'aws-lambda';
6+
import { Logger } from '@aws-lambda-powertools/logger';
7+
8+
const logger = new Logger();
9+
10+
export const handler: Handler<
11+
APIGatewayProxyEventV2,
12+
APIGatewayProxyResultV2 | void
13+
> = async (event, _context, callback) => {
14+
const host = event.headers.Host!;
15+
const {
16+
client_id, scope, state, response_type, redirect_uri,
17+
} = event.queryStringParameters || {};
18+
const redirectUri = `https://github.com/login/oauth/authorize?client_id=${client_id}&scope=${encodeURIComponent(
19+
scope!,
20+
)}${state ? `&state=${state}` : ''}&response_type=${response_type}${redirect_uri ? `&redirect_uri=${encodeURIComponent(redirect_uri)}` : ''}`;
21+
22+
logger.info(`Redirect to ${redirectUri}`);
23+
24+
callback(null, {
25+
statusCode: 302,
26+
headers: {
27+
Location: redirectUri,
28+
},
29+
});
30+
};

lambda/jwks/index.ts

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,23 @@
1-
import { Handler, APIGatewayProxyEventV2, APIGatewayProxyResultV2 } from 'aws-lambda';
1+
import {
2+
Handler,
3+
APIGatewayProxyEventV2,
4+
APIGatewayProxyResultV2,
5+
} from 'aws-lambda';
26
import { pem2jwk } from 'pem-jwk';
37
import * as fs from 'fs';
48

5-
export const handler: Handler<APIGatewayProxyEventV2, APIGatewayProxyResultV2> = async (event, _context, _callback) => {
9+
export const handler: Handler<
10+
APIGatewayProxyEventV2,
11+
APIGatewayProxyResultV2 | void
12+
> = async (_event, _context, callback) => {
613
const pem = fs.readFileSync('/var/task/jwtRS256.private.pem', 'ascii');
714
const jwk = pem2jwk(pem, {
815
alg: 'RS256',
916
kid: 'jwtRS256',
1017
});
11-
return {
12-
cookies: [],
18+
19+
callback(null, {
1320
statusCode: 200,
1421
body: JSON.stringify(jwk),
15-
};
16-
}
22+
});
23+
};
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import {
2+
Handler,
3+
APIGatewayProxyEventV2,
4+
APIGatewayProxyResultV2,
5+
} from 'aws-lambda';
6+
import { Logger } from '@aws-lambda-powertools/logger';
7+
8+
const logger = new Logger();
9+
10+
export const handler: Handler<
11+
APIGatewayProxyEventV2,
12+
APIGatewayProxyResultV2 | void
13+
> = async (event, _context, callback) => {
14+
const host = event.headers.Host!;
15+
const body = JSON.stringify({
16+
issuer: `https://${host}`,
17+
authorization_endpoint: `https://${host}/authorize`,
18+
token_endpoint: `https://${host}/token`,
19+
token_endpoint_auth_methods_supported: [
20+
'client_secret_basic',
21+
'private_key_jwt',
22+
],
23+
token_endpoint_auth_signing_alg_values_supported: ['RS256'],
24+
userinfo_endpoint: `https://${host}/userinfo`,
25+
jwks_uri: `https://${host}/.well-known/jwks.json`,
26+
scopes_supported: ['openid', 'read:user', 'user:email'],
27+
response_types_supported: [
28+
'code',
29+
'code id_token',
30+
'id_token',
31+
'token id_token',
32+
],
33+
subject_types_supported: ['public'],
34+
userinfo_signing_alg_values_supported: ['none'],
35+
id_token_signing_alg_values_supported: ['RS256'],
36+
request_object_signing_alg_values_supported: ['none'],
37+
display_values_supported: ['page', 'popup'],
38+
claims_supported: [
39+
'sub',
40+
'name',
41+
'preferred_username',
42+
'profile',
43+
'picture',
44+
'website',
45+
'email',
46+
'email_verified',
47+
'updated_at',
48+
'iss',
49+
'aud',
50+
],
51+
});
52+
53+
logger.info(`Response ${body}`);
54+
55+
callback(null, {
56+
statusCode: 200,
57+
body,
58+
});
59+
};

lambda/token/index.ts

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,23 @@
1-
import { Handler, APIGatewayProxyEventV2, APIGatewayProxyResultV2 } from 'aws-lambda';
1+
import {
2+
Handler,
3+
APIGatewayProxyEventV2,
4+
APIGatewayProxyResultV2,
5+
} from 'aws-lambda';
26
import fetch from 'cross-fetch';
3-
import { left, right, isRight } from 'fp-ts/Either'
4-
7+
import { left, right, isRight } from 'fp-ts/Either';
58

69
const eventToRequest = (source: string) => {
710
const bodyString = Buffer.from(source, 'base64').toString('ascii');
811
const body = new URLSearchParams(bodyString);
9-
const paramNames =[ 'grant_type', 'redirect_uri', 'client_id', 'client_secret', 'code' ];
12+
const paramNames = [
13+
'grant_type',
14+
'redirect_uri',
15+
'client_id',
16+
'client_secret',
17+
'code',
18+
];
1019

11-
const invalidParams = paramNames.filter(name => !body.has(name));
20+
const invalidParams = paramNames.filter((name) => !body.has(name));
1221
if (invalidParams.length > 0) {
1322
return right(() => `token request body ${invalidParams}`);
1423
}
@@ -21,35 +30,37 @@ const eventToRequest = (source: string) => {
2130
code: body.get('code')!,
2231
state: body.get('state') ?? undefined,
2332
}));
24-
}
33+
};
2534

26-
export const handler: Handler<APIGatewayProxyEventV2, APIGatewayProxyResultV2> = async (event, _context, _callback) => {
35+
export const handler: Handler<
36+
APIGatewayProxyEventV2,
37+
APIGatewayProxyResultV2 | void
38+
> = async (event, _context, callback) => {
2739
if (!event.body) {
28-
return {
29-
cookies: [],
40+
callback(null, {
3041
statusCode: 400,
31-
};
42+
});
43+
return;
3244
}
3345
const result = eventToRequest(event.body);
3446
if (isRight(result)) {
35-
return {
36-
cookies: [],
47+
callback(null, {
3748
statusCode: 400,
3849
body: result.right(),
39-
};
50+
});
51+
return;
4052
}
4153
const body = JSON.stringify(result.left());
4254
const response = await fetch('https://github.com/login/oauth/access_token', {
4355
method: 'POST',
4456
headers: {
4557
'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8',
46-
'Accept': 'application/json'
58+
Accept: 'application/json',
4759
},
48-
body
60+
body,
4961
});
50-
return {
51-
cookies: [],
62+
callback(null, {
5263
statusCode: 200,
5364
body: JSON.stringify(await response.json()),
54-
};
55-
}
65+
});
66+
};

0 commit comments

Comments
 (0)