@infoxchange/make-it-so 2.10.0-internal-testing-vdt-199-add-oidc-auth-3.1 → 2.11.0-internal-testing-vdt-199-add-ix-oidc-auth.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -304,7 +304,7 @@ const auth = new CloudFrontOidcAuth(stack, "CloudFrontOidcAuth", {
304
304
  });
305
305
 
306
306
  // Then you apply it to the a CloudFront backed site when it's created
307
- const site = new IxStaticSite(stack, "IxStaticSite", {
307
+ new IxStaticSite(stack, "IxStaticSite", {
308
308
  path: "path/to/site/files",
309
309
  cdk: {
310
310
  distribution: auth.addToDistributionDefinition(stack, {
@@ -314,6 +314,29 @@ const site = new IxStaticSite(stack, "IxStaticSite", {
314
314
  });
315
315
  ```
316
316
 
317
+ <details>
318
+ <summary><strong>ApiGatewayOidcAuth</strong> - Adds OIDC authentication to a API Gateway instance.</summary>
319
+
320
+ This is an instance of SST v2's [Auth construct](https://v2.sst.dev/auth) that is preconfigured for OIDC.
321
+
322
+ ```typescript
323
+ import { ApiGatewayOidcAuth } from "@infoxchange/make-it-so/cdk-constructs";
324
+
325
+ // You first create an instance of ApiGatewayOidcAuth
326
+ const auth = new ApiGatewayOidcAuth(stack, "ApiGatewayOidcAuth", {
327
+ oidcIssuerUrl: "https://your-oidc-server.com/path/",
328
+ oidcClientId: "your-client-id",
329
+ oidcScope: "email",
330
+ });
331
+
332
+ // Then you attach it to an API instance
333
+ const api = new Api(stack, "api", {});
334
+ auth.attach(stack, {
335
+ api,
336
+ prefix: "/auth", // optional
337
+ });
338
+ ```
339
+
317
340
  ## Full Example
318
341
 
319
342
  To deploy a Next.js based site you would include a `sst.config.ts` file at the root of repo with contents like this:
@@ -0,0 +1,9 @@
1
+ declare module "sst/node/auth" {
2
+ interface SessionTypes {
3
+ user: {
4
+ userID: string;
5
+ };
6
+ }
7
+ }
8
+ export declare const handler: (event: any, context: any) => Promise<any>;
9
+ //# sourceMappingURL=auth-route.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"auth-route.d.ts","sourceRoot":"","sources":["../../../src/cdk-constructs/ApiGatewayOidcAuth/auth-route.ts"],"names":[],"mappings":"AAoBA,OAAO,QAAQ,eAAe,CAAC;IAC7B,UAAiB,YAAY;QAC3B,IAAI,EAAE;YACJ,MAAM,EAAE,MAAM,CAAC;SAChB,CAAC;KACH;CACF;AAED,eAAO,MAAM,OAAO,4CAiBlB,CAAC"}
@@ -0,0 +1,33 @@
1
+ import { AuthHandler, OidcAdapter, Session } from "sst/node/auth";
2
+ import { Issuer } from "openid-client";
3
+ const oidcClientId = process.env.OIDC_CLIENT_ID;
4
+ if (!oidcClientId) {
5
+ throw new Error("OIDC_CLIENT_ID not set");
6
+ }
7
+ const oidcIssuerUrl = process.env.OIDC_ISSUER_URL;
8
+ if (!oidcIssuerUrl) {
9
+ throw new Error("OIDC_ISSUER_URL not set");
10
+ }
11
+ const oidcScope = process.env.OIDC_SCOPE;
12
+ if (!oidcScope) {
13
+ throw new Error("OIDC_SCOPE not set");
14
+ }
15
+ const oidcIssuerConfigUrl = new URL(`${process.env.OIDC_ISSUER_URL?.replace(/\/$/, "")}/.well-known/openid-configuration`);
16
+ export const handler = AuthHandler({
17
+ providers: {
18
+ oidc: OidcAdapter({
19
+ issuer: await Issuer.discover(oidcIssuerConfigUrl.href),
20
+ clientID: oidcClientId,
21
+ scope: oidcScope,
22
+ onSuccess: async (tokenset) => {
23
+ return Session.cookie({
24
+ redirect: "/",
25
+ type: "user",
26
+ properties: {
27
+ userID: tokenset.claims().sub,
28
+ },
29
+ });
30
+ },
31
+ }),
32
+ },
33
+ });
@@ -0,0 +1,13 @@
1
+ import * as SST from "sst/constructs";
2
+ type ConstructScope = ConstructorParameters<typeof SST.Auth>[0];
3
+ type ConstructId = ConstructorParameters<typeof SST.Auth>[1];
4
+ type Props = Omit<SST.AuthProps, "authenticator"> & {
5
+ oidcIssuerUrl: string;
6
+ oidcClientId: string;
7
+ oidcScope: string;
8
+ };
9
+ export declare class ApiGatewayOidcAuth extends SST.Auth {
10
+ constructor(scope: ConstructScope, id: ConstructId, props: Props);
11
+ }
12
+ export {};
13
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/cdk-constructs/ApiGatewayOidcAuth/index.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,GAAG,MAAM,gBAAgB,CAAC;AAGtC,KAAK,cAAc,GAAG,qBAAqB,CAAC,OAAO,GAAG,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC;AAChE,KAAK,WAAW,GAAG,qBAAqB,CAAC,OAAO,GAAG,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC;AAE7D,KAAK,KAAK,GAAG,IAAI,CAAC,GAAG,CAAC,SAAS,EAAE,eAAe,CAAC,GAAG;IAClD,aAAa,EAAE,MAAM,CAAC;IACtB,YAAY,EAAE,MAAM,CAAC;IACrB,SAAS,EAAE,MAAM,CAAC;CACnB,CAAC;AAEF,qBAAa,kBAAmB,SAAQ,GAAG,CAAC,IAAI;gBAClC,KAAK,EAAE,cAAc,EAAE,EAAE,EAAE,WAAW,EAAE,KAAK,EAAE,KAAK;CAajE"}
@@ -0,0 +1,17 @@
1
+ import * as SST from "sst/constructs";
2
+ import path from "node:path";
3
+ export class ApiGatewayOidcAuth extends SST.Auth {
4
+ constructor(scope, id, props) {
5
+ super(scope, id, {
6
+ ...props,
7
+ authenticator: {
8
+ handler: path.join(import.meta.dirname, "auth-route.handler"),
9
+ environment: {
10
+ OIDC_ISSUER_URL: props.oidcIssuerUrl,
11
+ OIDC_CLIENT_ID: props.oidcClientId,
12
+ OIDC_SCOPE: props.oidcScope,
13
+ },
14
+ },
15
+ });
16
+ }
17
+ }
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../src/cdk-constructs/CloudFrontOidcAuth/index.ts"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vC,OAAO,EAAE,4BAA4B,EAAE,MAAM,4BAA4B,CAAC;AAI1E,KAAK,cAAc,GAAG,qBAAqB,CAAC,OAAO,SAAS,CAAC,CAAC,CAAC,CAAC,CAAC;AACjE,KAAK,WAAW,GAAG,qBAAqB,CAAC,OAAO,SAAS,CAAC,CAAC,CAAC,CAAC,CAAC;AAE9D,KAAK,OAAO,CAAC,CAAC,IAAI;IAChB,CAAC,UAAU,CAAC,IAAI,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;CAC/B,CAAC;AAEF,KAAK,KAAK,GAAG;IACX,aAAa,EAAE,MAAM,CAAC;IACtB,YAAY,EAAE,MAAM,CAAC;IACrB,SAAS,EAAE,MAAM,CAAC;CACnB,CAAC;AAEF,qBAAa,kBAAmB,SAAQ,SAAS;IAC/C,QAAQ,CAAC,aAAa,EAAE,MAAM,CAAC;IAC/B,QAAQ,CAAC,YAAY,EAAE,MAAM,CAAC;IAC9B,QAAQ,CAAC,SAAS,EAAE,MAAM,CAAC;IAC3B,QAAQ,CAAC,EAAE,EAAE,MAAM,CAAC;gBAER,KAAK,EAAE,cAAc,EAAE,EAAE,EAAE,WAAW,EAAE,KAAK,EAAE,KAAK;IAQhE,2BAA2B,CACzB,iBAAiB,SAAS,4BAA4B,EAEtD,KAAK,EAAE,cAAc,EACrB,EACE,sBAAsB,EACtB,MAAgB,GACjB,EAAE;QAAE,sBAAsB,EAAE,OAAO,CAAC,iBAAiB,CAAC,CAAC;QAAC,MAAM,CAAC,EAAE,MAAM,CAAA;KAAE;IAwC5E,OAAO,CAAC,sBAAsB;IAgI9B,OAAO,CAAC,sBAAsB;CA8E/B"}
@@ -54,6 +54,7 @@ export class CloudFrontOidcAuth extends Construct {
54
54
  this.getAuthBehaviorOptions(scope, jwtSecret, prefix);
55
55
  return updatedDistributionDefinition;
56
56
  }
57
+ // This deals with the infra required for checking if requests are authenticated and redirecting to the auth route if not
57
58
  getFunctionAssociation(scope, jwtSecret) {
58
59
  const cfKeyValueStore = new CloudFront.KeyValueStore(scope, `${this.id}CFKeyValueStore`);
59
60
  const kvStoreId = cfKeyValueStore.keyValueStoreId; // Your KV store ID
@@ -137,6 +138,8 @@ export class CloudFrontOidcAuth extends Construct {
137
138
  eventType: CloudFront.FunctionEventType.VIEWER_REQUEST,
138
139
  };
139
140
  }
141
+ // This deals with the infra required for handling the OIDC authorisation process for requests that aren't yet
142
+ // authenticated but want to become authenticated.
140
143
  getAuthBehaviorOptions(scope, jwtSecret, prefix) {
141
144
  const authRouteFunction = new SST.Function(scope, `${this.id}AuthRouteFunction`, {
142
145
  runtime: "nodejs20.x",
@@ -7,4 +7,5 @@ export * from "./IxElasticache.js";
7
7
  export * from "./IxApi.js";
8
8
  export * from "./IxQuicksightWorkspace.js";
9
9
  export * from "./CloudFrontOidcAuth/index.js";
10
+ export * from "./ApiGatewayOidcAuth/index.js";
10
11
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/cdk-constructs/index.ts"],"names":[],"mappings":"AAAA,cAAc,mBAAmB,CAAC;AAClC,cAAc,oBAAoB,CAAC;AACnC,cAAc,kBAAkB,CAAC;AACjC,cAAc,mBAAmB,CAAC;AAClC,cAAc,mBAAmB,CAAC;AAClC,cAAc,oBAAoB,CAAC;AACnC,cAAc,YAAY,CAAC;AAC3B,cAAc,4BAA4B,CAAC;AAC3C,cAAc,+BAA+B,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/cdk-constructs/index.ts"],"names":[],"mappings":"AAAA,cAAc,mBAAmB,CAAC;AAClC,cAAc,oBAAoB,CAAC;AACnC,cAAc,kBAAkB,CAAC;AACjC,cAAc,mBAAmB,CAAC;AAClC,cAAc,mBAAmB,CAAC;AAClC,cAAc,oBAAoB,CAAC;AACnC,cAAc,YAAY,CAAC;AAC3B,cAAc,4BAA4B,CAAC;AAC3C,cAAc,+BAA+B,CAAC;AAC9C,cAAc,+BAA+B,CAAC"}
@@ -7,3 +7,4 @@ export * from "./IxElasticache.js";
7
7
  export * from "./IxApi.js";
8
8
  export * from "./IxQuicksightWorkspace.js";
9
9
  export * from "./CloudFrontOidcAuth/index.js";
10
+ export * from "./ApiGatewayOidcAuth/index.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@infoxchange/make-it-so",
3
- "version": "2.10.0-internal-testing-vdt-199-add-oidc-auth-3.1",
3
+ "version": "2.11.0-internal-testing-vdt-199-add-ix-oidc-auth.1",
4
4
  "description": "Makes deploying services to IX infra easy",
5
5
  "repository": "github:infoxchange/make-it-so",
6
6
  "type": "module",
@@ -0,0 +1,46 @@
1
+ import { AuthHandler, OidcAdapter, Session } from "sst/node/auth";
2
+ import { Issuer } from "openid-client";
3
+
4
+ const oidcClientId = process.env.OIDC_CLIENT_ID;
5
+ if (!oidcClientId) {
6
+ throw new Error("OIDC_CLIENT_ID not set");
7
+ }
8
+ const oidcIssuerUrl = process.env.OIDC_ISSUER_URL;
9
+ if (!oidcIssuerUrl) {
10
+ throw new Error("OIDC_ISSUER_URL not set");
11
+ }
12
+ const oidcScope = process.env.OIDC_SCOPE;
13
+ if (!oidcScope) {
14
+ throw new Error("OIDC_SCOPE not set");
15
+ }
16
+
17
+ const oidcIssuerConfigUrl = new URL(
18
+ `${process.env.OIDC_ISSUER_URL?.replace(/\/$/, "")}/.well-known/openid-configuration`,
19
+ );
20
+
21
+ declare module "sst/node/auth" {
22
+ export interface SessionTypes {
23
+ user: {
24
+ userID: string;
25
+ };
26
+ }
27
+ }
28
+
29
+ export const handler = AuthHandler({
30
+ providers: {
31
+ oidc: OidcAdapter({
32
+ issuer: await Issuer.discover(oidcIssuerConfigUrl.href),
33
+ clientID: oidcClientId,
34
+ scope: oidcScope,
35
+ onSuccess: async (tokenset) => {
36
+ return Session.cookie({
37
+ redirect: "/",
38
+ type: "user",
39
+ properties: {
40
+ userID: tokenset.claims().sub,
41
+ },
42
+ });
43
+ },
44
+ }),
45
+ },
46
+ });
@@ -0,0 +1,27 @@
1
+ import * as SST from "sst/constructs";
2
+ import path from "node:path";
3
+
4
+ type ConstructScope = ConstructorParameters<typeof SST.Auth>[0];
5
+ type ConstructId = ConstructorParameters<typeof SST.Auth>[1];
6
+
7
+ type Props = Omit<SST.AuthProps, "authenticator"> & {
8
+ oidcIssuerUrl: string;
9
+ oidcClientId: string;
10
+ oidcScope: string;
11
+ };
12
+
13
+ export class ApiGatewayOidcAuth extends SST.Auth {
14
+ constructor(scope: ConstructScope, id: ConstructId, props: Props) {
15
+ super(scope, id, {
16
+ ...props,
17
+ authenticator: {
18
+ handler: path.join(import.meta.dirname, "auth-route.handler"),
19
+ environment: {
20
+ OIDC_ISSUER_URL: props.oidcIssuerUrl,
21
+ OIDC_CLIENT_ID: props.oidcClientId,
22
+ OIDC_SCOPE: props.oidcScope,
23
+ },
24
+ },
25
+ });
26
+ }
27
+ }
@@ -84,6 +84,7 @@ export class CloudFrontOidcAuth extends Construct {
84
84
  return updatedDistributionDefinition;
85
85
  }
86
86
 
87
+ // This deals with the infra required for checking if requests are authenticated and redirecting to the auth route if not
87
88
  private getFunctionAssociation(
88
89
  scope: ConstructScope,
89
90
  jwtSecret: SecretsManager.Secret,
@@ -210,6 +211,8 @@ export class CloudFrontOidcAuth extends Construct {
210
211
  };
211
212
  }
212
213
 
214
+ // This deals with the infra required for handling the OIDC authorisation process for requests that aren't yet
215
+ // authenticated but want to become authenticated.
213
216
  private getAuthBehaviorOptions(
214
217
  scope: ConstructScope,
215
218
  jwtSecret: SecretsManager.Secret,
@@ -7,3 +7,4 @@ export * from "./IxElasticache.js";
7
7
  export * from "./IxApi.js";
8
8
  export * from "./IxQuicksightWorkspace.js";
9
9
  export * from "./CloudFrontOidcAuth/index.js";
10
+ export * from "./ApiGatewayOidcAuth/index.js";