@shware/security 4.1.0 → 5.0.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.
Files changed (78) hide show
  1. package/README.md +110 -99
  2. package/dist/auth/index.cjs +41 -34
  3. package/dist/auth/index.cjs.map +1 -1
  4. package/dist/auth/index.d.cts +6 -6
  5. package/dist/auth/index.d.cts.map +1 -1
  6. package/dist/auth/index.d.mts +6 -6
  7. package/dist/auth/index.d.mts.map +1 -1
  8. package/dist/auth/index.mjs +41 -34
  9. package/dist/auth/index.mjs.map +1 -1
  10. package/dist/auth/types.d.cts +12 -7
  11. package/dist/auth/types.d.cts.map +1 -1
  12. package/dist/auth/types.d.mts +12 -7
  13. package/dist/auth/types.d.mts.map +1 -1
  14. package/dist/index.cjs +2 -2
  15. package/dist/index.d.cts +2 -2
  16. package/dist/index.d.mts +2 -2
  17. package/dist/index.mjs +2 -2
  18. package/dist/oauth2/client.cjs +111 -48
  19. package/dist/oauth2/client.cjs.map +1 -1
  20. package/dist/oauth2/client.d.cts +51 -13
  21. package/dist/oauth2/client.d.cts.map +1 -1
  22. package/dist/oauth2/client.d.mts +51 -13
  23. package/dist/oauth2/client.d.mts.map +1 -1
  24. package/dist/oauth2/client.mjs +111 -48
  25. package/dist/oauth2/client.mjs.map +1 -1
  26. package/dist/oauth2/provider/apple-client-secret.cjs +3 -3
  27. package/dist/oauth2/provider/apple-client-secret.cjs.map +1 -1
  28. package/dist/oauth2/provider/apple-client-secret.d.cts +3 -3
  29. package/dist/oauth2/provider/apple-client-secret.d.mts +3 -3
  30. package/dist/oauth2/provider/apple-client-secret.mjs +3 -3
  31. package/dist/oauth2/provider/apple-client-secret.mjs.map +1 -1
  32. package/dist/oauth2/provider/apple.cjs +9 -3
  33. package/dist/oauth2/provider/apple.cjs.map +1 -1
  34. package/dist/oauth2/provider/apple.d.cts.map +1 -1
  35. package/dist/oauth2/provider/apple.d.mts.map +1 -1
  36. package/dist/oauth2/provider/apple.mjs +9 -3
  37. package/dist/oauth2/provider/apple.mjs.map +1 -1
  38. package/dist/oauth2/provider/common.cjs +14 -9
  39. package/dist/oauth2/provider/common.cjs.map +1 -1
  40. package/dist/oauth2/provider/common.d.cts +15 -2
  41. package/dist/oauth2/provider/common.d.cts.map +1 -1
  42. package/dist/oauth2/provider/common.d.mts +15 -2
  43. package/dist/oauth2/provider/common.d.mts.map +1 -1
  44. package/dist/oauth2/provider/common.mjs +14 -9
  45. package/dist/oauth2/provider/common.mjs.map +1 -1
  46. package/dist/oauth2/provider/facebook.cjs +59 -35
  47. package/dist/oauth2/provider/facebook.cjs.map +1 -1
  48. package/dist/oauth2/provider/facebook.d.cts.map +1 -1
  49. package/dist/oauth2/provider/facebook.d.mts.map +1 -1
  50. package/dist/oauth2/provider/facebook.mjs +59 -35
  51. package/dist/oauth2/provider/facebook.mjs.map +1 -1
  52. package/dist/oauth2/provider/github.cjs +27 -5
  53. package/dist/oauth2/provider/github.cjs.map +1 -1
  54. package/dist/oauth2/provider/github.d.cts.map +1 -1
  55. package/dist/oauth2/provider/github.d.mts.map +1 -1
  56. package/dist/oauth2/provider/github.mjs +27 -5
  57. package/dist/oauth2/provider/github.mjs.map +1 -1
  58. package/dist/oauth2/provider/google.cjs +31 -32
  59. package/dist/oauth2/provider/google.cjs.map +1 -1
  60. package/dist/oauth2/provider/google.d.cts.map +1 -1
  61. package/dist/oauth2/provider/google.d.mts.map +1 -1
  62. package/dist/oauth2/provider/google.mjs +31 -32
  63. package/dist/oauth2/provider/google.mjs.map +1 -1
  64. package/dist/oauth2/provider/index.cjs +1 -1
  65. package/dist/oauth2/provider/index.mjs +1 -1
  66. package/dist/oauth2/provider/wechat.cjs +1 -0
  67. package/dist/oauth2/provider/wechat.cjs.map +1 -1
  68. package/dist/oauth2/provider/wechat.d.cts.map +1 -1
  69. package/dist/oauth2/provider/wechat.d.mts.map +1 -1
  70. package/dist/oauth2/provider/wechat.mjs +1 -0
  71. package/dist/oauth2/provider/wechat.mjs.map +1 -1
  72. package/dist/oauth2/types.cjs.map +1 -1
  73. package/dist/oauth2/types.d.cts +69 -34
  74. package/dist/oauth2/types.d.cts.map +1 -1
  75. package/dist/oauth2/types.d.mts +69 -34
  76. package/dist/oauth2/types.d.mts.map +1 -1
  77. package/dist/oauth2/types.mjs.map +1 -1
  78. package/package.json +1 -1
@@ -1,8 +1,14 @@
1
1
  import { OAuth2Error } from "../error.mjs";
2
2
  import { createAuthorizationUri, exchangeAuthorizationCode, verifyIdToken } from "./common.mjs";
3
+ import { createHmac } from "node:crypto";
3
4
  import { invariant } from "@shware/utils";
4
5
  //#region src/oauth2/provider/facebook.ts
5
6
  /**
7
+ * `iss` of Limited Login id tokens, per https://limited.facebook.com/.well-known/openid-configuration/
8
+ * (their `aud` is the app id).
9
+ */
10
+ const FACEBOOK_ISSUER = "https://www.facebook.com";
11
+ /**
6
12
  * ref: https://developers.facebook.com/docs/facebook-login/guides/advanced/manual-flow/
7
13
  * ref: https://developers.facebook.com/tools/explorer
8
14
  * */
@@ -32,10 +38,14 @@ function createFacebookProvider() {
32
38
  }
33
39
  return await response.json();
34
40
  },
35
- async getUserInfo({ id_token, access_token }) {
41
+ async getUserInfo({ id_token, access_token }, { clientId }) {
36
42
  invariant(this.jwkSetUri, "jwkSetUri is required");
37
43
  if (id_token) {
38
- const data = await verifyIdToken(id_token, this.jwkSetUri);
44
+ const data = await verifyIdToken(id_token, {
45
+ jwkSetUri: this.jwkSetUri,
46
+ issuer: FACEBOOK_ISSUER,
47
+ audience: clientId
48
+ });
39
49
  return {
40
50
  data,
41
51
  claims: {
@@ -49,45 +59,59 @@ function createFacebookProvider() {
49
59
  }
50
60
  invariant(access_token, "access_token is required");
51
61
  invariant(this.userInfoUri, "userInfoUri is required");
52
- const url = new URL(this.userInfoUri);
53
- const fields = [
54
- "id",
55
- "name",
56
- "email",
57
- "picture.width(320).height(320)"
58
- ];
59
- url.searchParams.append("access_token", access_token);
60
- url.searchParams.append("fields", fields.join(","));
61
- const response = await fetch(url.href, { headers: { Accept: "application/json" } });
62
- if (!response.ok) throw new OAuth2Error(response.status, "invalid_request", "Failed to fetch user info");
63
- const data = await response.json();
64
- return {
65
- data,
66
- claims: {
67
- sub: data.id,
68
- name: data.name,
69
- email: data.email,
70
- picture: data.picture.data.url,
71
- email_verified: data.email_verified
72
- }
73
- };
62
+ return fetchUserInfo(this.userInfoUri, access_token);
74
63
  },
75
- async loginOAuth2Native({ credentials }) {
76
- if (credentials.id_token || credentials.access_token) {
77
- const userInfo = await this.getUserInfo(credentials);
78
- return {
79
- token: {
80
- token_type: "bearer",
81
- access_token: credentials.access_token ?? "",
82
- id_token: credentials.id_token
83
- },
84
- userInfo
85
- };
64
+ async loginOAuth2Native({ clientId, clientSecret, credentials }) {
65
+ const { id_token, access_token } = credentials;
66
+ let userInfo;
67
+ if (id_token) userInfo = await this.getUserInfo({
68
+ token_type: "bearer",
69
+ access_token: "",
70
+ id_token
71
+ }, { clientId });
72
+ else if (access_token) {
73
+ if (!clientSecret) throw new OAuth2Error(400, "invalid_client", "access_token login needs the app secret");
74
+ invariant(this.userInfoUri, "userInfoUri is required");
75
+ const proof = createHmac("sha256", clientSecret).update(access_token).digest("hex");
76
+ userInfo = await fetchUserInfo(this.userInfoUri, access_token, proof);
86
77
  } else throw new OAuth2Error(400, "invalid_request", "id_token or access_token is required");
78
+ return {
79
+ token: {
80
+ token_type: "bearer",
81
+ access_token: access_token ?? "",
82
+ id_token
83
+ },
84
+ userInfo
85
+ };
87
86
  }
88
87
  };
89
88
  }
90
89
  const facebook = createFacebookProvider();
90
+ async function fetchUserInfo(userInfoUri, accessToken, appSecretProof) {
91
+ const url = new URL(userInfoUri);
92
+ const fields = [
93
+ "id",
94
+ "name",
95
+ "email",
96
+ "picture.width(320).height(320)"
97
+ ];
98
+ url.searchParams.append("access_token", accessToken);
99
+ url.searchParams.append("fields", fields.join(","));
100
+ if (appSecretProof) url.searchParams.append("appsecret_proof", appSecretProof);
101
+ const response = await fetch(url.href, { headers: { Accept: "application/json" } });
102
+ if (!response.ok) throw new OAuth2Error(response.status, "invalid_request", "Failed to fetch user info");
103
+ const data = await response.json();
104
+ return {
105
+ data,
106
+ claims: {
107
+ sub: data.id,
108
+ name: data.name,
109
+ email: data.email,
110
+ picture: data.picture.data.url,
111
+ email_verified: data.email_verified
112
+ }
113
+ };
114
+ }
91
115
  //#endregion
92
116
  export { createFacebookProvider, facebook };
93
117
 
@@ -1 +1 @@
1
- {"version":3,"file":"facebook.mjs","names":[],"sources":["../../../src/oauth2/provider/facebook.ts"],"sourcesContent":["import { invariant } from '@shware/utils';\nimport { OAuth2Error } from '../error';\nimport type { LoginOAuth2NativeParams, OAuth2Token, Provider } from '../types';\nimport { createAuthorizationUri, exchangeAuthorizationCode, verifyIdToken } from './common';\n\n/**\n * ref: https://developers.facebook.com/docs/facebook-login/guides/advanced/manual-flow/\n * ref: https://developers.facebook.com/tools/explorer\n * */\nexport function createFacebookProvider(): Provider {\n return {\n authorizationUri: 'https://www.facebook.com/v21.0/dialog/oauth',\n tokenUri: 'https://graph.facebook.com/v19.0/oauth/access_token',\n userInfoUri: 'https://graph.facebook.com/me',\n jwkSetUri: 'https://limited.facebook.com/.well-known/oauth/openid/jwks/',\n userNameAttribute: 'id',\n defaultScope: ['profile', 'email'],\n createAuthorizationUri({ pkce: _, ...params }) {\n return createAuthorizationUri({\n ...params,\n scope: params.scope ?? this.defaultScope,\n authorizationUri: this.authorizationUri,\n });\n },\n async exchangeAuthorizationCode({ pkce: _, ...params }) {\n const response = await exchangeAuthorizationCode({ ...params, tokenUri: this.tokenUri });\n if (!response.ok) {\n const { error } = (await response.json()) as FacebookErrorResponse;\n throw new OAuth2Error(response.status, 'invalid_request', error.message);\n }\n return (await response.json()) as FacebookToken;\n },\n async getUserInfo({ id_token, access_token }) {\n invariant(this.jwkSetUri, 'jwkSetUri is required');\n if (id_token) {\n const data = await verifyIdToken<FacebookDecodedIdToken>(id_token, this.jwkSetUri);\n return {\n data,\n claims: {\n sub: data.sub,\n name: data.name,\n picture: data.picture,\n email: data.email,\n email_verified: true,\n },\n };\n }\n invariant(access_token, 'access_token is required');\n invariant(this.userInfoUri, 'userInfoUri is required');\n\n const url = new URL(this.userInfoUri);\n const fields = ['id', 'name', 'email', 'picture.width(320).height(320)'];\n url.searchParams.append('access_token', access_token);\n url.searchParams.append('fields', fields.join(','));\n const headers = { Accept: 'application/json' };\n const response = await fetch(url.href, { headers });\n if (!response.ok) {\n throw new OAuth2Error(response.status, 'invalid_request', 'Failed to fetch user info');\n }\n const data = (await response.json()) as FacebookUserInfo;\n return {\n data,\n claims: {\n sub: data.id,\n name: data.name,\n email: data.email,\n picture: data.picture.data.url,\n email_verified: data.email_verified,\n },\n };\n },\n async loginOAuth2Native({ credentials }: LoginOAuth2NativeParams) {\n if (credentials.id_token || credentials.access_token) {\n const userInfo = await this.getUserInfo(credentials as unknown as OAuth2Token);\n const token: OAuth2Token = {\n token_type: 'bearer',\n access_token: credentials.access_token ?? '',\n id_token: credentials.id_token,\n };\n return { token, userInfo };\n } else {\n throw new OAuth2Error(400, 'invalid_request', 'id_token or access_token is required');\n }\n },\n };\n}\n\nexport const facebook = createFacebookProvider();\n\nexport interface FacebookDecodedIdToken {\n sub: string;\n name: string;\n email: string;\n picture: string;\n nonce?: string;\n}\n\nexport interface FacebookUserInfo {\n id: string;\n name: string;\n email: string;\n email_verified: boolean;\n picture: {\n data: {\n url: string;\n width: number;\n height: number;\n is_silhouette: boolean;\n };\n };\n}\n\nexport interface FacebookToken extends OAuth2Token {\n access_token: string;\n token_type: 'bearer';\n expires_in: number;\n id_token?: string;\n}\n\nexport type FacebookAppCredential =\n | { state: string; id_token: string } // platform=ios and tracking=limited\n | { state: string; access_token: string }; // tracking=enabled\n\ninterface FacebookErrorResponse {\n error: {\n message: string;\n type: 'OAuthException';\n code: number;\n error_subcode: number;\n fbtrace_id: string;\n };\n}\n"],"mappings":";;;;;;;;AASA,SAAgB,yBAAmC;CACjD,OAAO;EACL,kBAAk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AK;GAClC,OAAO;IACL;IACA,QAAQ;KACN,KAAK,KAAK;KACV,MAAM,KAAK;KACX,OAAO,KAAK;KACZ,SAAS,KAAK,QAAQ,KAAK;KAC3B,gBAAgB,KAAK;IACvB;GACF;EACF;EACA,MAAM,kBAAkB,EAAE,eAAwC;GAChE,IAAI,YAAY,YAAY,YAAY,cAAc;IACpD,MAAM,WAAW,MAAM,KAAK,YAAY,WAAqC;IAM7E,OAAO;KAAE,OAAA;MAJP,YAAY;MACZ,cAAc,YAAY,gBAAgB;MAC1C,UAAU,YAAY;KAEX;KAAG;IAAS;GAC3B,OACE,MAAM,IAAI,YAAY,KAAK,mBAAmB,sCAAsC;EAExF;CACF;AACF;AAEA,MAAa,WAAW,uBAAuB"}
1
+ {"version":3,"file":"facebook.mjs","names":[],"sources":["../../../src/oauth2/provider/facebook.ts"],"sourcesContent":["import { createHmac } from 'node:crypto';\nimport { invariant } from '@shware/utils';\nimport { OAuth2Error } from '../error';\nimport type { LoginOAuth2NativeParams, OAuth2Token, Provider, UserInfo } from '../types';\nimport { createAuthorizationUri, exchangeAuthorizationCode, verifyIdToken } from './common';\n\n/**\n * `iss` of Limited Login id tokens, per https://limited.facebook.com/.well-known/openid-configuration/\n * (their `aud` is the app id).\n */\nconst FACEBOOK_ISSUER = 'https://www.facebook.com';\n\n/**\n * ref: https://developers.facebook.com/docs/facebook-login/guides/advanced/manual-flow/\n * ref: https://developers.facebook.com/tools/explorer\n * */\nexport function createFacebookProvider(): Provider {\n return {\n authorizationUri: 'https://www.facebook.com/v21.0/dialog/oauth',\n tokenUri: 'https://graph.facebook.com/v19.0/oauth/access_token',\n userInfoUri: 'https://graph.facebook.com/me',\n jwkSetUri: 'https://limited.facebook.com/.well-known/oauth/openid/jwks/',\n userNameAttribute: 'id',\n defaultScope: ['profile', 'email'],\n createAuthorizationUri({ pkce: _, ...params }) {\n return createAuthorizationUri({\n ...params,\n scope: params.scope ?? this.defaultScope,\n authorizationUri: this.authorizationUri,\n });\n },\n async exchangeAuthorizationCode({ pkce: _, ...params }) {\n const response = await exchangeAuthorizationCode({ ...params, tokenUri: this.tokenUri });\n if (!response.ok) {\n const { error } = (await response.json()) as FacebookErrorResponse;\n throw new OAuth2Error(response.status, 'invalid_request', error.message);\n }\n return (await response.json()) as FacebookToken;\n },\n async getUserInfo({ id_token, access_token }, { clientId }) {\n invariant(this.jwkSetUri, 'jwkSetUri is required');\n if (id_token) {\n const data = await verifyIdToken<FacebookDecodedIdToken>(id_token, {\n jwkSetUri: this.jwkSetUri,\n issuer: FACEBOOK_ISSUER,\n audience: clientId,\n });\n return {\n data,\n claims: {\n sub: data.sub,\n name: data.name,\n picture: data.picture,\n email: data.email,\n email_verified: true,\n },\n };\n }\n invariant(access_token, 'access_token is required');\n invariant(this.userInfoUri, 'userInfoUri is required');\n return fetchUserInfo(this.userInfoUri, access_token);\n },\n async loginOAuth2Native({ clientId, clientSecret, credentials }: LoginOAuth2NativeParams) {\n const { id_token, access_token } = credentials;\n let userInfo: UserInfo;\n if (id_token) {\n // Limited Login: the id token's `aud` must be this app.\n userInfo = await this.getUserInfo(\n { token_type: 'bearer', access_token: '', id_token },\n {\n clientId,\n }\n );\n } else if (access_token) {\n // The Graph API serves any app's user access token, so `/me` alone would accept a token a\n // foreign app obtained for the same user. With `appsecret_proof` Facebook refuses tokens\n // not issued to the app whose secret signed the proof, binding the token to this client.\n if (!clientSecret) {\n throw new OAuth2Error(400, 'invalid_client', 'access_token login needs the app secret');\n }\n invariant(this.userInfoUri, 'userInfoUri is required');\n const proof = createHmac('sha256', clientSecret).update(access_token).digest('hex');\n userInfo = await fetchUserInfo(this.userInfoUri, access_token, proof);\n } else {\n throw new OAuth2Error(400, 'invalid_request', 'id_token or access_token is required');\n }\n const token: OAuth2Token = {\n token_type: 'bearer',\n access_token: access_token ?? '',\n id_token,\n };\n return { token, userInfo };\n },\n };\n}\n\nexport const facebook = createFacebookProvider();\n\nasync function fetchUserInfo(\n userInfoUri: string,\n accessToken: string,\n appSecretProof?: string\n): Promise<UserInfo<FacebookUserInfo>> {\n const url = new URL(userInfoUri);\n const fields = ['id', 'name', 'email', 'picture.width(320).height(320)'];\n url.searchParams.append('access_token', accessToken);\n url.searchParams.append('fields', fields.join(','));\n if (appSecretProof) url.searchParams.append('appsecret_proof', appSecretProof);\n const headers = { Accept: 'application/json' };\n const response = await fetch(url.href, { headers });\n if (!response.ok) {\n throw new OAuth2Error(response.status, 'invalid_request', 'Failed to fetch user info');\n }\n const data = (await response.json()) as FacebookUserInfo;\n return {\n data,\n claims: {\n sub: data.id,\n name: data.name,\n email: data.email,\n picture: data.picture.data.url,\n email_verified: data.email_verified,\n },\n };\n}\n\nexport interface FacebookDecodedIdToken {\n sub: string;\n name: string;\n email: string;\n picture: string;\n nonce?: string;\n}\n\nexport interface FacebookUserInfo {\n id: string;\n name: string;\n email: string;\n email_verified: boolean;\n picture: {\n data: {\n url: string;\n width: number;\n height: number;\n is_silhouette: boolean;\n };\n };\n}\n\nexport interface FacebookToken extends OAuth2Token {\n access_token: string;\n token_type: 'bearer';\n expires_in: number;\n id_token?: string;\n}\n\nexport type FacebookAppCredential =\n | { state: string; id_token: string } // platform=ios and tracking=limited\n | { state: string; access_token: string }; // tracking=enabled\n\ninterface FacebookErrorResponse {\n error: {\n message: string;\n type: 'OAuthException';\n code: number;\n error_subcode: number;\n fbtrace_id: string;\n };\n}\n"],"mappings":";;;;;;;;;AAUA,MAAM,kBAAkB;;;;;AAMxB,SAAgB,yBAAmC;CACjD,OAAO;EACL,kBAAkB;EAClB,UAAU;EACV,aAAa;EACb,WAAW;EACX,mBAAmB;EACnB,cAAc,CAAC,WAAW,OAAO;EACjC,uBAAuB,EAAE,MAAM,GAAG,GAAG,UAAU;GAC7C,OAAO,uBAAuB;IAC5B,GAAG;IACH,OAAO,OAAO,SAAS,KAAK;IAC5B,kBAAkB,KAAK;GACzB,CAAC;EACH;EACA,MAAM,0BAA0B,EAAE,MAAM,GAAG,GAAG,UAAU;GACtD,MAAM,WA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F;AACF;AAEA,MAAa,WAAW,uBAAuB;AAE/C,eAAe,cACb,aACA,aACA,gBACqC;CACrC,MAAM,MAAM,IAAI,IAAI,WAAW;CAC/B,MAAM,SAAS;EAAC;EAAM;EAAQ;EAAS;CAAgC;CACvE,IAAI,aAAa,OAAO,gBAAgB,WAAW;CACnD,IAAI,aAAa,OAAO,UAAU,OAAO,KAAK,GAAG,CAAC;CAClD,IAAI,gBAAgB,IAAI,aAAa,OAAO,mBAAmB,cAAc;CAE7E,MAAM,WAAW,MAAM,MAAM,IAAI,MAAM,EAAE,SAAA,EADvB,QAAQ,mBACqB,EAAE,CAAC;CAClD,IAAI,CAAC,SAAS,IACZ,MAAM,IAAI,YAAY,SAAS,QAAQ,mBAAmB,2BAA2B;CAEvF,MAAM,OAAQ,MAAM,SAAS,KAAK;CAClC,OAAO;EACL;EACA,QAAQ;GACN,KAAK,KAAK;GACV,MAAM,KAAK;GACX,OAAO,KAAK;GACZ,SAAS,KAAK,QAAQ,KAAK;GAC3B,gBAAgB,KAAK;EACvB;CACF;AACF"}
@@ -3,6 +3,18 @@ const require_oauth2_error = require("../error.cjs");
3
3
  const require_oauth2_provider_common = require("./common.cjs");
4
4
  let _shware_utils = require("@shware/utils");
5
5
  //#region src/oauth2/provider/github.ts
6
+ /**
7
+ * GitHub's REST API rejects any request without a `User-Agent` header with a 403. Node's fetch sends
8
+ * a default one, but Cloudflare Workers' fetch sends none, so every GitHub request sets it explicitly.
9
+ * reference: https://docs.github.com/en/rest/using-the-rest-api/troubleshooting-the-rest-api#user-agent-required
10
+ */
11
+ const USER_AGENT = "@shware/security";
12
+ /** A short slice of an error response body, enough to tell GitHub's failures apart in logs. */
13
+ async function describeFailure(response, message) {
14
+ const body = await response.text().catch(() => "");
15
+ const detail = body ? `: ${body.slice(0, 200)}` : "";
16
+ return `${message} (HTTP ${response.status})${detail}`;
17
+ }
6
18
  function createGithubProvider() {
7
19
  return {
8
20
  authorizationUri: "https://github.com/login/oauth/authorize",
@@ -20,26 +32,36 @@ function createGithubProvider() {
20
32
  async exchangeAuthorizationCode({ pkce: _, ...params }) {
21
33
  const response = await require_oauth2_provider_common.exchangeAuthorizationCode({
22
34
  ...params,
23
- tokenUri: this.tokenUri
35
+ tokenUri: this.tokenUri,
36
+ headers: { "User-Agent": USER_AGENT }
24
37
  });
25
38
  if (!response.ok) {
26
39
  const { error, error_description } = await response.json();
27
40
  throw new require_oauth2_error.OAuth2Error(response.status, error, error_description);
28
41
  }
29
- return await response.json();
42
+ const body = await response.json();
43
+ if (body.error) throw new require_oauth2_error.OAuth2Error(400, body.error, body.error_description);
44
+ return body;
30
45
  },
31
46
  async getUserInfo({ access_token }) {
32
47
  (0, _shware_utils.invariant)(access_token, "access_token is required");
33
48
  (0, _shware_utils.invariant)(this.userInfoUri, "userInfoUri is required");
34
49
  const headers = {
35
50
  Accept: "application/json",
36
- Authorization: `Bearer ${access_token}`
51
+ Authorization: `Bearer ${access_token}`,
52
+ "User-Agent": USER_AGENT
37
53
  };
38
54
  const response = await fetch(this.userInfoUri, { headers });
39
- if (!response.ok) throw new require_oauth2_error.OAuth2Error(response.status, "invalid_request", "Failed to fetch user info");
55
+ if (!response.ok) {
56
+ const message = await describeFailure(response, "Failed to fetch user info");
57
+ throw new require_oauth2_error.OAuth2Error(response.status, "invalid_request", message);
58
+ }
40
59
  const data = await response.json();
41
60
  const emailResponse = await fetch("https://api.github.com/user/emails", { headers });
42
- if (!emailResponse.ok) throw new require_oauth2_error.OAuth2Error(emailResponse.status, "invalid_request", "Failed to fetch user email");
61
+ if (!emailResponse.ok) {
62
+ const message = await describeFailure(emailResponse, "Failed to fetch user email");
63
+ throw new require_oauth2_error.OAuth2Error(emailResponse.status, "invalid_request", message);
64
+ }
43
65
  const emails = await emailResponse.json();
44
66
  data.email = emails.find((email) => email.primary)?.email || data.email;
45
67
  const email_verified = emails.find((email) => email.email === data.email)?.verified ?? false;
@@ -1 +1 @@
1
- {"version":3,"file":"github.cjs","names":["createAuthorizationUri","exchangeAuthorizationCode","OAuth2Error"],"sources":["../../../src/oauth2/provider/github.ts"],"sourcesContent":["import { invariant } from '@shware/utils';\nimport { OAuth2Error } from '../error';\nimport type { OAuth2Token, Provider } from '../types';\nimport { createAuthorizationUri, exchangeAuthorizationCode } from './common';\n\nexport function createGithubProvider(): Provider {\n return {\n authorizationUri: 'https://github.com/login/oauth/authorize',\n tokenUri: 'https://github.com/login/oauth/access_token',\n userInfoUri: 'https://api.github.com/user',\n userNameAttribute: 'id',\n defaultScope: ['user:email'],\n createAuthorizationUri({ pkce: _, ...params }) {\n return createAuthorizationUri({\n ...params,\n scope: params.scope ?? this.defaultScope,\n authorizationUri: this.authorizationUri,\n });\n },\n async exchangeAuthorizationCode({ pkce: _, ...params }) {\n const response = await exchangeAuthorizationCode({ ...params, tokenUri: this.tokenUri });\n if (!response.ok) {\n const { error, error_description } = (await response.json()) as GithubErrorResponse;\n throw new OAuth2Error(response.status, error, error_description);\n }\n return (await response.json()) as GithubToken;\n },\n async getUserInfo({ access_token }) {\n invariant(access_token, 'access_token is required');\n invariant(this.userInfoUri, 'userInfoUri is required');\n const headers = { Accept: 'application/json', Authorization: `Bearer ${access_token}` };\n const response = await fetch(this.userInfoUri, { headers });\n if (!response.ok) {\n throw new OAuth2Error(response.status, 'invalid_request', 'Failed to fetch user info');\n }\n const data = (await response.json()) as GithubUserInfo;\n const emailResponse = await fetch('https://api.github.com/user/emails', { headers });\n if (!emailResponse.ok) {\n throw new OAuth2Error(\n emailResponse.status,\n 'invalid_request',\n 'Failed to fetch user email'\n );\n }\n const emails = (await emailResponse.json()) as GithubEmail[];\n data.email = emails.find((email) => email.primary)?.email || data.email;\n const email_verified = emails.find((email) => email.email === data.email)?.verified ?? false;\n return {\n data,\n claims: {\n sub: data.id,\n name: data.name || data.login,\n email_verified,\n email: data.email,\n picture: data.avatar_url,\n profile: data.html_url,\n website: data.html_url,\n updated_at: data.updated_at ? Date.parse(data.updated_at) : undefined,\n },\n };\n },\n };\n}\n\nexport const github = createGithubProvider();\n\nexport interface GithubUserInfo {\n login: string;\n id: string;\n node_id: string;\n avatar_url: string;\n gravatar_id: string;\n url: string;\n html_url: string;\n followers_url: string;\n following_url: string;\n gists_url: string;\n starred_url: string;\n subscriptions_url: string;\n organizations_url: string;\n repos_url: string;\n events_url: string;\n received_events_url: string;\n type: string;\n site_admin: boolean;\n name: string;\n company: string;\n blog: string;\n location: string;\n email: string;\n hireable: boolean;\n bio: string;\n twitter_username: string;\n public_repos: string;\n public_gists: string;\n followers: string;\n following: string;\n created_at: string;\n updated_at: string;\n private_gists: string;\n total_private_repos: string;\n owned_private_repos: string;\n disk_usage: string;\n collaborators: string;\n two_factor_authentication: boolean;\n plan: {\n name: string;\n space: string;\n private_repos: string;\n collaborators: string;\n };\n}\n\ninterface GithubEmail {\n email: string;\n primary: boolean;\n verified: boolean;\n visibility: 'public' | 'private';\n}\n\nexport interface GithubToken extends OAuth2Token {\n access_token: string;\n token_type: string;\n scope: string;\n expires_in: number;\n refresh_token: string;\n}\n\ninterface GithubErrorResponse {\n error: string;\n error_description: string;\n error_uri: string;\n}\n"],"mappings":";;;;;AAKA,SAAgB,uBAAiC;CAC/C,OAAO;EACL,kBAAkB;EAClB,UAAU;EACV,aAAa;EACb,mBAAmB;EACnB,cAAc,CAAC,YAAY;EAC3B,uBAAuB,EAAE,MAAM,GAAG,GAAG,UAAU;GAC7C,OAAOA,+BAAAA,uBAAuB;IAC5B,GAAG;IACH,OAAO,OAAO,SAAS,KAAK;IAC5B,kBAAkB,KAAK;GACzB,CAAC;EACH;EACA,MAAM,0BAA0B,EAAE,MAAM,GAAG,GAAG,UAAU;GACtD,MAAM,WAAW,MAAMC,+BAAAA,0BAA0B;IAAE,GAAG;IAAQ,UAAU,KAAK;GAAS,CAAC;GACvF,IAAI,CAAC,SAAS,IAAI;IAChB,MAAM,EAAE,OAAO,sBAAuB,MAAM,SAAS,KAAK;IAC1D,MAAM,IAAIC,qBAAAA,YAAY,SAAS,QAAQ,OAAO,iBAAiB;GACjE;GACA,OAAQ,MAAM,SAAS,KAAK;EAC9B;EACA,MAAM,YAAY,EAAE,gBAAgB;GAClC,CAAA,GAAA,cAAA,UAAA,CAAU,cAAc,0BAA0B;GAClD,CAAA,GAAA,cAAA,UAAA,CAAU,KAAK,aAAa,yBAAyB;GACrD,MAAM,UAAU;IAAE,QAAQ;IAAoB,eAAe,UAAU;GAAe;GACtF,MAAM,WAAW,MAAM,MAAM,KAAK,aAAa,EAAE,QAAQ,CAAC;GAC1D,IAAI,CAAC,SAAS,IACZ,MAAM,IAAIA,qBAAAA,YAAY,SAAS,QAAQ,mBAAmB,2BAA2B;GAEvF,MAAM,OAAQ,MAAM,SAAS,KAAK;GAClC,MAAM,gBAAgB,MAAM,MAAM,sCAAsC,EAAE,QAAQ,CAAC;GACnF,IAAI,CAAC,cAAc,IACjB,MAAM,IAAIA,qBAAAA,YACR,cAAc,QACd,mBACA,4BACF;GAEF,MAAM,SAAU,MAAM,cAAc,KAAK;GACzC,KAAK,QAAQ,OAAO,MAAM,UAAU,MAAM,OAAO,CAAC,EAAE,SAAS,KAAK;GAClE,MAAM,iBAAiB,OAAO,MAAM,UAAU,MAAM,UAAU,KAAK,KAAK,CAAC,EAAE,YAAY;GACvF,OAAO;IACL;IACA,QAAQ;KACN,KAAK,KAAK;KACV,MAAM,KAAK,QAAQ,KAAK;KACxB;KACA,OAAO,KAAK;KACZ,SAAS,KAAK;KACd,SAAS,KAAK;KACd,SAAS,KAAK;KACd,YAAY,KAAK,aAAa,KAAK,MAAM,KAAK,UAAU,IAAI,KAAA;IAC9D;GACF;EACF;CACF;AACF;AAEA,MAAa,SAAS,qBAAqB"}
1
+ {"version":3,"file":"github.cjs","names":["createAuthorizationUri","exchangeAuthorizationCode","OAuth2Error"],"sources":["../../../src/oauth2/provider/github.ts"],"sourcesContent":["import { invariant } from '@shware/utils';\nimport { OAuth2Error } from '../error';\nimport type { OAuth2Token, Provider } from '../types';\nimport { createAuthorizationUri, exchangeAuthorizationCode } from './common';\n\n/**\n * GitHub's REST API rejects any request without a `User-Agent` header with a 403. Node's fetch sends\n * a default one, but Cloudflare Workers' fetch sends none, so every GitHub request sets it explicitly.\n * reference: https://docs.github.com/en/rest/using-the-rest-api/troubleshooting-the-rest-api#user-agent-required\n */\nconst USER_AGENT = '@shware/security';\n\n/** A short slice of an error response body, enough to tell GitHub's failures apart in logs. */\nasync function describeFailure(response: Response, message: string) {\n const body = await response.text().catch(() => '');\n const detail = body ? `: ${body.slice(0, 200)}` : '';\n return `${message} (HTTP ${response.status})${detail}`;\n}\n\nexport function createGithubProvider(): Provider {\n return {\n authorizationUri: 'https://github.com/login/oauth/authorize',\n tokenUri: 'https://github.com/login/oauth/access_token',\n userInfoUri: 'https://api.github.com/user',\n userNameAttribute: 'id',\n defaultScope: ['user:email'],\n createAuthorizationUri({ pkce: _, ...params }) {\n return createAuthorizationUri({\n ...params,\n scope: params.scope ?? this.defaultScope,\n authorizationUri: this.authorizationUri,\n });\n },\n async exchangeAuthorizationCode({ pkce: _, ...params }) {\n const response = await exchangeAuthorizationCode({\n ...params,\n tokenUri: this.tokenUri,\n headers: { 'User-Agent': USER_AGENT },\n });\n if (!response.ok) {\n const { error, error_description } = (await response.json()) as GithubErrorResponse;\n throw new OAuth2Error(response.status, error, error_description);\n }\n // GitHub answers a failed exchange (bad_verification_code, incorrect_client_credentials, ...)\n // with HTTP 200 and an error body, so a 200 alone does not mean a token was issued.\n const body = (await response.json()) as GithubToken & Partial<GithubErrorResponse>;\n if (body.error) throw new OAuth2Error(400, body.error, body.error_description);\n return body;\n },\n async getUserInfo({ access_token }) {\n invariant(access_token, 'access_token is required');\n invariant(this.userInfoUri, 'userInfoUri is required');\n const headers = {\n Accept: 'application/json',\n Authorization: `Bearer ${access_token}`,\n 'User-Agent': USER_AGENT,\n };\n const response = await fetch(this.userInfoUri, { headers });\n if (!response.ok) {\n const message = await describeFailure(response, 'Failed to fetch user info');\n throw new OAuth2Error(response.status, 'invalid_request', message);\n }\n const data = (await response.json()) as GithubUserInfo;\n const emailResponse = await fetch('https://api.github.com/user/emails', { headers });\n if (!emailResponse.ok) {\n const message = await describeFailure(emailResponse, 'Failed to fetch user email');\n throw new OAuth2Error(emailResponse.status, 'invalid_request', message);\n }\n const emails = (await emailResponse.json()) as GithubEmail[];\n data.email = emails.find((email) => email.primary)?.email || data.email;\n const email_verified = emails.find((email) => email.email === data.email)?.verified ?? false;\n return {\n data,\n claims: {\n sub: data.id,\n name: data.name || data.login,\n email_verified,\n email: data.email,\n picture: data.avatar_url,\n profile: data.html_url,\n website: data.html_url,\n updated_at: data.updated_at ? Date.parse(data.updated_at) : undefined,\n },\n };\n },\n };\n}\n\nexport const github = createGithubProvider();\n\nexport interface GithubUserInfo {\n login: string;\n id: string;\n node_id: string;\n avatar_url: string;\n gravatar_id: string;\n url: string;\n html_url: string;\n followers_url: string;\n following_url: string;\n gists_url: string;\n starred_url: string;\n subscriptions_url: string;\n organizations_url: string;\n repos_url: string;\n events_url: string;\n received_events_url: string;\n type: string;\n site_admin: boolean;\n name: string;\n company: string;\n blog: string;\n location: string;\n email: string;\n hireable: boolean;\n bio: string;\n twitter_username: string;\n public_repos: string;\n public_gists: string;\n followers: string;\n following: string;\n created_at: string;\n updated_at: string;\n private_gists: string;\n total_private_repos: string;\n owned_private_repos: string;\n disk_usage: string;\n collaborators: string;\n two_factor_authentication: boolean;\n plan: {\n name: string;\n space: string;\n private_repos: string;\n collaborators: string;\n };\n}\n\ninterface GithubEmail {\n email: string;\n primary: boolean;\n verified: boolean;\n visibility: 'public' | 'private';\n}\n\nexport interface GithubToken extends OAuth2Token {\n access_token: string;\n token_type: string;\n scope: string;\n expires_in: number;\n refresh_token: string;\n}\n\ninterface GithubErrorResponse {\n error: string;\n error_description: string;\n error_uri: string;\n}\n"],"mappings":";;;;;;;;;;AAUA,MAAM,aAAa;;AAGnB,eAAe,gBAAgB,UAAoB,SAAiB;CAClE,MAAM,OAAO,MAAM,SAAS,KAAK,CAAC,CAAC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,mBAAmB,OAAO;GACnE;GACA,MAAM,OAAQ,MAAM,SAAS,KAAK;GAClC,MAAM,gBAAgB,MAAM,MAAM,sCAAsC,EAAE,QAAQ,CAAC;GACnF,IAAI,CAAC,cAAc,IAAI;IACrB,MAAM,UAAU,MAAM,gBAAgB,eAAe,4BAA4B;IACjF,MAAM,IAAIA,qBAAAA,YAAY,cAAc,QAAQ,mBAAmB,OAAO;GACxE;GACA,MAAM,SAAU,MAAM,cAAc,KAAK;GACzC,KAAK,QAAQ,OAAO,MAAM,UAAU,MAAM,OAAO,CAAC,EAAE,SAAS,KAAK;GAClE,MAAM,iBAAiB,OAAO,MAAM,UAAU,MAAM,UAAU,KAAK,KAAK,CAAC,EAAE,YAAY;GACvF,OAAO;IACL;IACA,QAAQ;KACN,KAAK,KAAK;KACV,MAAM,KAAK,QAAQ,KAAK;KACxB;KACA,OAAO,KAAK;KACZ,SAAS,KAAK;KACd,SAAS,KAAK;KACd,SAAS,KAAK;KACd,YAAY,KAAK,aAAa,KAAK,MAAM,KAAK,UAAU,IAAI,KAAA;IAC9D;GACF;EACF;CACF;AACF;AAEA,MAAa,SAAS,qBAAqB"}
@@ -1 +1 @@
1
- {"version":3,"file":"github.d.cts","names":[],"sources":["../../../src/oauth2/provider/github.ts"],"mappings":";;wBAKgB,wBAAwB;qBA2D3B,QAAM;iBAEF;EACf;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;IACE;IACA;IACA;IACA;;;iBAWa,oBAAoB;EACnC;EACA;EACA;EACA;EACA"}
1
+ {"version":3,"file":"github.d.cts","names":[],"sources":["../../../src/oauth2/provider/github.ts"],"mappings":";;wBAmBgB,wBAAwB;qBAqE3B,QAAM;iBAEF;EACf;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;IACE;IACA;IACA;IACA;;;iBAWa,oBAAoB;EACnC;EACA;EACA;EACA;EACA"}
@@ -1 +1 @@
1
- {"version":3,"file":"github.d.mts","names":[],"sources":["../../../src/oauth2/provider/github.ts"],"mappings":";;wBAKgB,wBAAwB;qBA2D3B,QAAM;iBAEF;EACf;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;IACE;IACA;IACA;IACA;;;iBAWa,oBAAoB;EACnC;EACA;EACA;EACA;EACA"}
1
+ {"version":3,"file":"github.d.mts","names":[],"sources":["../../../src/oauth2/provider/github.ts"],"mappings":";;wBAmBgB,wBAAwB;qBAqE3B,QAAM;iBAEF;EACf;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;IACE;IACA;IACA;IACA;;;iBAWa,oBAAoB;EACnC;EACA;EACA;EACA;EACA"}
@@ -2,6 +2,18 @@ import { OAuth2Error } from "../error.mjs";
2
2
  import { createAuthorizationUri, exchangeAuthorizationCode } from "./common.mjs";
3
3
  import { invariant } from "@shware/utils";
4
4
  //#region src/oauth2/provider/github.ts
5
+ /**
6
+ * GitHub's REST API rejects any request without a `User-Agent` header with a 403. Node's fetch sends
7
+ * a default one, but Cloudflare Workers' fetch sends none, so every GitHub request sets it explicitly.
8
+ * reference: https://docs.github.com/en/rest/using-the-rest-api/troubleshooting-the-rest-api#user-agent-required
9
+ */
10
+ const USER_AGENT = "@shware/security";
11
+ /** A short slice of an error response body, enough to tell GitHub's failures apart in logs. */
12
+ async function describeFailure(response, message) {
13
+ const body = await response.text().catch(() => "");
14
+ const detail = body ? `: ${body.slice(0, 200)}` : "";
15
+ return `${message} (HTTP ${response.status})${detail}`;
16
+ }
5
17
  function createGithubProvider() {
6
18
  return {
7
19
  authorizationUri: "https://github.com/login/oauth/authorize",
@@ -19,26 +31,36 @@ function createGithubProvider() {
19
31
  async exchangeAuthorizationCode({ pkce: _, ...params }) {
20
32
  const response = await exchangeAuthorizationCode({
21
33
  ...params,
22
- tokenUri: this.tokenUri
34
+ tokenUri: this.tokenUri,
35
+ headers: { "User-Agent": USER_AGENT }
23
36
  });
24
37
  if (!response.ok) {
25
38
  const { error, error_description } = await response.json();
26
39
  throw new OAuth2Error(response.status, error, error_description);
27
40
  }
28
- return await response.json();
41
+ const body = await response.json();
42
+ if (body.error) throw new OAuth2Error(400, body.error, body.error_description);
43
+ return body;
29
44
  },
30
45
  async getUserInfo({ access_token }) {
31
46
  invariant(access_token, "access_token is required");
32
47
  invariant(this.userInfoUri, "userInfoUri is required");
33
48
  const headers = {
34
49
  Accept: "application/json",
35
- Authorization: `Bearer ${access_token}`
50
+ Authorization: `Bearer ${access_token}`,
51
+ "User-Agent": USER_AGENT
36
52
  };
37
53
  const response = await fetch(this.userInfoUri, { headers });
38
- if (!response.ok) throw new OAuth2Error(response.status, "invalid_request", "Failed to fetch user info");
54
+ if (!response.ok) {
55
+ const message = await describeFailure(response, "Failed to fetch user info");
56
+ throw new OAuth2Error(response.status, "invalid_request", message);
57
+ }
39
58
  const data = await response.json();
40
59
  const emailResponse = await fetch("https://api.github.com/user/emails", { headers });
41
- if (!emailResponse.ok) throw new OAuth2Error(emailResponse.status, "invalid_request", "Failed to fetch user email");
60
+ if (!emailResponse.ok) {
61
+ const message = await describeFailure(emailResponse, "Failed to fetch user email");
62
+ throw new OAuth2Error(emailResponse.status, "invalid_request", message);
63
+ }
42
64
  const emails = await emailResponse.json();
43
65
  data.email = emails.find((email) => email.primary)?.email || data.email;
44
66
  const email_verified = emails.find((email) => email.email === data.email)?.verified ?? false;
@@ -1 +1 @@
1
- {"version":3,"file":"github.mjs","names":[],"sources":["../../../src/oauth2/provider/github.ts"],"sourcesContent":["import { invariant } from '@shware/utils';\nimport { OAuth2Error } from '../error';\nimport type { OAuth2Token, Provider } from '../types';\nimport { createAuthorizationUri, exchangeAuthorizationCode } from './common';\n\nexport function createGithubProvider(): Provider {\n return {\n authorizationUri: 'https://github.com/login/oauth/authorize',\n tokenUri: 'https://github.com/login/oauth/access_token',\n userInfoUri: 'https://api.github.com/user',\n userNameAttribute: 'id',\n defaultScope: ['user:email'],\n createAuthorizationUri({ pkce: _, ...params }) {\n return createAuthorizationUri({\n ...params,\n scope: params.scope ?? this.defaultScope,\n authorizationUri: this.authorizationUri,\n });\n },\n async exchangeAuthorizationCode({ pkce: _, ...params }) {\n const response = await exchangeAuthorizationCode({ ...params, tokenUri: this.tokenUri });\n if (!response.ok) {\n const { error, error_description } = (await response.json()) as GithubErrorResponse;\n throw new OAuth2Error(response.status, error, error_description);\n }\n return (await response.json()) as GithubToken;\n },\n async getUserInfo({ access_token }) {\n invariant(access_token, 'access_token is required');\n invariant(this.userInfoUri, 'userInfoUri is required');\n const headers = { Accept: 'application/json', Authorization: `Bearer ${access_token}` };\n const response = await fetch(this.userInfoUri, { headers });\n if (!response.ok) {\n throw new OAuth2Error(response.status, 'invalid_request', 'Failed to fetch user info');\n }\n const data = (await response.json()) as GithubUserInfo;\n const emailResponse = await fetch('https://api.github.com/user/emails', { headers });\n if (!emailResponse.ok) {\n throw new OAuth2Error(\n emailResponse.status,\n 'invalid_request',\n 'Failed to fetch user email'\n );\n }\n const emails = (await emailResponse.json()) as GithubEmail[];\n data.email = emails.find((email) => email.primary)?.email || data.email;\n const email_verified = emails.find((email) => email.email === data.email)?.verified ?? false;\n return {\n data,\n claims: {\n sub: data.id,\n name: data.name || data.login,\n email_verified,\n email: data.email,\n picture: data.avatar_url,\n profile: data.html_url,\n website: data.html_url,\n updated_at: data.updated_at ? Date.parse(data.updated_at) : undefined,\n },\n };\n },\n };\n}\n\nexport const github = createGithubProvider();\n\nexport interface GithubUserInfo {\n login: string;\n id: string;\n node_id: string;\n avatar_url: string;\n gravatar_id: string;\n url: string;\n html_url: string;\n followers_url: string;\n following_url: string;\n gists_url: string;\n starred_url: string;\n subscriptions_url: string;\n organizations_url: string;\n repos_url: string;\n events_url: string;\n received_events_url: string;\n type: string;\n site_admin: boolean;\n name: string;\n company: string;\n blog: string;\n location: string;\n email: string;\n hireable: boolean;\n bio: string;\n twitter_username: string;\n public_repos: string;\n public_gists: string;\n followers: string;\n following: string;\n created_at: string;\n updated_at: string;\n private_gists: string;\n total_private_repos: string;\n owned_private_repos: string;\n disk_usage: string;\n collaborators: string;\n two_factor_authentication: boolean;\n plan: {\n name: string;\n space: string;\n private_repos: string;\n collaborators: string;\n };\n}\n\ninterface GithubEmail {\n email: string;\n primary: boolean;\n verified: boolean;\n visibility: 'public' | 'private';\n}\n\nexport interface GithubToken extends OAuth2Token {\n access_token: string;\n token_type: string;\n scope: string;\n expires_in: number;\n refresh_token: string;\n}\n\ninterface GithubErrorResponse {\n error: string;\n error_description: string;\n error_uri: string;\n}\n"],"mappings":";;;;AAKA,SAAgB,uBAAiC;CAC/C,OAAO;EACL,kBAAkB;EAClB,UAAU;EACV,aAAa;EACb,mBAAmB;EACnB,cAAc,CAAC,YAAY;EAC3B,uBAAuB,EAAE,MAAM,GAAG,GAAG,UAAU;GAC7C,OAAO,uBAAuB;IAC5B,GAAG;IACH,OAAO,OAAO,SAAS,KAAK;IAC5B,kBAAkB,KAAK;GACzB,CAAC;EACH;EACA,MAAM,0BAA0B,EAAE,MAAM,GAAG,GAAG,UAAU;GACtD,MAAM,WAAW,MAAM,0BAA0B;IAAE,GAAG;IAAQ,UAAU,KAAK;GAAS,CAAC;GACvF,IAAI,CAAC,SAAS,IAAI;IAChB,MAAM,EAAE,OAAO,sBAAuB,MAAM,SAAS,KAAK;IAC1D,MAAM,IAAI,YAAY,SAAS,QAAQ,OAAO,iBAAiB;GACjE;GACA,OAAQ,MAAM,SAAS,KAAK;EAC9B;EACA,MAAM,YAAY,EAAE,gBAAgB;GAClC,UAAU,cAAc,0BAA0B;GAClD,UAAU,KAAK,aAAa,yBAAyB;GACrD,MAAM,UAAU;IAAE,QAAQ;IAAoB,eAAe,UAAU;GAAe;GACtF,MAAM,WAAW,MAAM,MAAM,KAAK,aAAa,EAAE,QAAQ,CAAC;GAC1D,IAAI,CAAC,SAAS,IACZ,MAAM,IAAI,YAAY,SAAS,QAAQ,mBAAmB,2BAA2B;GAEvF,MAAM,OAAQ,MAAM,SAAS,KAAK;GAClC,MAAM,gBAAgB,MAAM,MAAM,sCAAsC,EAAE,QAAQ,CAAC;GACnF,IAAI,CAAC,cAAc,IACjB,MAAM,IAAI,YACR,cAAc,QACd,mBACA,4BACF;GAEF,MAAM,SAAU,MAAM,cAAc,KAAK;GACzC,KAAK,QAAQ,OAAO,MAAM,UAAU,MAAM,OAAO,CAAC,EAAE,SAAS,KAAK;GAClE,MAAM,iBAAiB,OAAO,MAAM,UAAU,MAAM,UAAU,KAAK,KAAK,CAAC,EAAE,YAAY;GACvF,OAAO;IACL;IACA,QAAQ;KACN,KAAK,KAAK;KACV,MAAM,KAAK,QAAQ,KAAK;KACxB;KACA,OAAO,KAAK;KACZ,SAAS,KAAK;KACd,SAAS,KAAK;KACd,SAAS,KAAK;KACd,YAAY,KAAK,aAAa,KAAK,MAAM,KAAK,UAAU,IAAI,KAAA;IAC9D;GACF;EACF;CACF;AACF;AAEA,MAAa,SAAS,qBAAqB"}
1
+ {"version":3,"file":"github.mjs","names":[],"sources":["../../../src/oauth2/provider/github.ts"],"sourcesContent":["import { invariant } from '@shware/utils';\nimport { OAuth2Error } from '../error';\nimport type { OAuth2Token, Provider } from '../types';\nimport { createAuthorizationUri, exchangeAuthorizationCode } from './common';\n\n/**\n * GitHub's REST API rejects any request without a `User-Agent` header with a 403. Node's fetch sends\n * a default one, but Cloudflare Workers' fetch sends none, so every GitHub request sets it explicitly.\n * reference: https://docs.github.com/en/rest/using-the-rest-api/troubleshooting-the-rest-api#user-agent-required\n */\nconst USER_AGENT = '@shware/security';\n\n/** A short slice of an error response body, enough to tell GitHub's failures apart in logs. */\nasync function describeFailure(response: Response, message: string) {\n const body = await response.text().catch(() => '');\n const detail = body ? `: ${body.slice(0, 200)}` : '';\n return `${message} (HTTP ${response.status})${detail}`;\n}\n\nexport function createGithubProvider(): Provider {\n return {\n authorizationUri: 'https://github.com/login/oauth/authorize',\n tokenUri: 'https://github.com/login/oauth/access_token',\n userInfoUri: 'https://api.github.com/user',\n userNameAttribute: 'id',\n defaultScope: ['user:email'],\n createAuthorizationUri({ pkce: _, ...params }) {\n return createAuthorizationUri({\n ...params,\n scope: params.scope ?? this.defaultScope,\n authorizationUri: this.authorizationUri,\n });\n },\n async exchangeAuthorizationCode({ pkce: _, ...params }) {\n const response = await exchangeAuthorizationCode({\n ...params,\n tokenUri: this.tokenUri,\n headers: { 'User-Agent': USER_AGENT },\n });\n if (!response.ok) {\n const { error, error_description } = (await response.json()) as GithubErrorResponse;\n throw new OAuth2Error(response.status, error, error_description);\n }\n // GitHub answers a failed exchange (bad_verification_code, incorrect_client_credentials, ...)\n // with HTTP 200 and an error body, so a 200 alone does not mean a token was issued.\n const body = (await response.json()) as GithubToken & Partial<GithubErrorResponse>;\n if (body.error) throw new OAuth2Error(400, body.error, body.error_description);\n return body;\n },\n async getUserInfo({ access_token }) {\n invariant(access_token, 'access_token is required');\n invariant(this.userInfoUri, 'userInfoUri is required');\n const headers = {\n Accept: 'application/json',\n Authorization: `Bearer ${access_token}`,\n 'User-Agent': USER_AGENT,\n };\n const response = await fetch(this.userInfoUri, { headers });\n if (!response.ok) {\n const message = await describeFailure(response, 'Failed to fetch user info');\n throw new OAuth2Error(response.status, 'invalid_request', message);\n }\n const data = (await response.json()) as GithubUserInfo;\n const emailResponse = await fetch('https://api.github.com/user/emails', { headers });\n if (!emailResponse.ok) {\n const message = await describeFailure(emailResponse, 'Failed to fetch user email');\n throw new OAuth2Error(emailResponse.status, 'invalid_request', message);\n }\n const emails = (await emailResponse.json()) as GithubEmail[];\n data.email = emails.find((email) => email.primary)?.email || data.email;\n const email_verified = emails.find((email) => email.email === data.email)?.verified ?? false;\n return {\n data,\n claims: {\n sub: data.id,\n name: data.name || data.login,\n email_verified,\n email: data.email,\n picture: data.avatar_url,\n profile: data.html_url,\n website: data.html_url,\n updated_at: data.updated_at ? Date.parse(data.updated_at) : undefined,\n },\n };\n },\n };\n}\n\nexport const github = createGithubProvider();\n\nexport interface GithubUserInfo {\n login: string;\n id: string;\n node_id: string;\n avatar_url: string;\n gravatar_id: string;\n url: string;\n html_url: string;\n followers_url: string;\n following_url: string;\n gists_url: string;\n starred_url: string;\n subscriptions_url: string;\n organizations_url: string;\n repos_url: string;\n events_url: string;\n received_events_url: string;\n type: string;\n site_admin: boolean;\n name: string;\n company: string;\n blog: string;\n location: string;\n email: string;\n hireable: boolean;\n bio: string;\n twitter_username: string;\n public_repos: string;\n public_gists: string;\n followers: string;\n following: string;\n created_at: string;\n updated_at: string;\n private_gists: string;\n total_private_repos: string;\n owned_private_repos: string;\n disk_usage: string;\n collaborators: string;\n two_factor_authentication: boolean;\n plan: {\n name: string;\n space: string;\n private_repos: string;\n collaborators: string;\n };\n}\n\ninterface GithubEmail {\n email: string;\n primary: boolean;\n verified: boolean;\n visibility: 'public' | 'private';\n}\n\nexport interface GithubToken extends OAuth2Token {\n access_token: string;\n token_type: string;\n scope: string;\n expires_in: number;\n refresh_token: string;\n}\n\ninterface GithubErrorResponse {\n error: string;\n error_description: string;\n error_uri: string;\n}\n"],"mappings":";;;;;;;;;AAUA,MAAM,aAAa;;AAGnB,eAAe,gBAAgB,UAAoB,SAAiB;CAClE,MAAM,OAAO,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AM,gBAAgB,MAAM,MAAM,sCAAsC,EAAE,QAAQ,CAAC;GACnF,IAAI,CAAC,cAAc,IAAI;IACrB,MAAM,UAAU,MAAM,gBAAgB,eAAe,4BAA4B;IACjF,MAAM,IAAI,YAAY,cAAc,QAAQ,mBAAmB,OAAO;GACxE;GACA,MAAM,SAAU,MAAM,cAAc,KAAK;GACzC,KAAK,QAAQ,OAAO,MAAM,UAAU,MAAM,OAAO,CAAC,EAAE,SAAS,KAAK;GAClE,MAAM,iBAAiB,OAAO,MAAM,UAAU,MAAM,UAAU,KAAK,KAAK,CAAC,EAAE,YAAY;GACvF,OAAO;IACL;IACA,QAAQ;KACN,KAAK,KAAK;KACV,MAAM,KAAK,QAAQ,KAAK;KACxB;KACA,OAAO,KAAK;KACZ,SAAS,KAAK;KACd,SAAS,KAAK;KACd,SAAS,KAAK;KACd,YAAY,KAAK,aAAa,KAAK,MAAM,KAAK,UAAU,IAAI,KAAA;IAC9D;GACF;EACF;CACF;AACF;AAEA,MAAa,SAAS,qBAAqB"}
@@ -3,9 +3,19 @@ const require_oauth2_error = require("../error.cjs");
3
3
  const require_oauth2_provider_common = require("./common.cjs");
4
4
  let _shware_utils = require("@shware/utils");
5
5
  //#region src/oauth2/provider/google.ts
6
- /** The Google Cloud project number every client id of a project starts with: `<number>-<hash>.apps.googleusercontent.com`. */
7
- function projectNumber(clientId) {
8
- return clientId.split("-", 1)[0];
6
+ /** Google signs id tokens with either `iss`, the URL or the bare host. */
7
+ const GOOGLE_ISSUERS = ["https://accounts.google.com", "accounts.google.com"];
8
+ function claims(data) {
9
+ return {
10
+ sub: data.sub,
11
+ name: data.name,
12
+ picture: data.picture,
13
+ email: data.email,
14
+ email_verified: data.email_verified,
15
+ given_name: data.given_name,
16
+ family_name: data.family_name,
17
+ locale: data.locale
18
+ };
9
19
  }
10
20
  function createGoogleProvider() {
11
21
  return {
@@ -37,35 +47,29 @@ function createGoogleProvider() {
37
47
  }
38
48
  return await response.json();
39
49
  },
40
- async getUserInfo({ id_token }) {
50
+ async getUserInfo({ id_token }, { clientId }) {
41
51
  (0, _shware_utils.invariant)(id_token, "id_token is required");
42
52
  (0, _shware_utils.invariant)(this.jwkSetUri, "jwkSetUri is required");
43
- const data = await require_oauth2_provider_common.verifyIdToken(id_token, this.jwkSetUri);
53
+ const data = await require_oauth2_provider_common.verifyIdToken(id_token, {
54
+ jwkSetUri: this.jwkSetUri,
55
+ issuer: GOOGLE_ISSUERS,
56
+ audience: clientId
57
+ });
44
58
  return {
45
59
  data,
46
- claims: {
47
- sub: data.sub,
48
- name: data.name,
49
- picture: data.picture,
50
- email: data.email,
51
- email_verified: data.email_verified,
52
- given_name: data.given_name,
53
- family_name: data.family_name,
54
- locale: data.locale
55
- }
60
+ claims: claims(data)
56
61
  };
57
62
  },
58
63
  async loginOAuth2Native({ pkce: _, credentials, ...params }) {
59
64
  (0, _shware_utils.invariant)(credentials.code, "code is required");
60
65
  const { code } = credentials;
61
66
  const { tokenUri } = this;
62
- const { client_id = params.clientId, redirect_uri = params.redirectUri } = credentials;
63
- if (projectNumber(client_id) !== projectNumber(params.clientId)) throw new require_oauth2_error.OAuth2Error(400, "invalid_client", "client_id belongs to another project");
67
+ const { redirect_uri = params.redirectUri } = credentials;
64
68
  const response = await require_oauth2_provider_common.exchangeAuthorizationCode({
65
69
  code,
66
70
  tokenUri,
67
- clientId: client_id,
68
- clientSecret: client_id === params.clientId ? params.clientSecret : "",
71
+ clientId: params.clientId,
72
+ clientSecret: params.clientSecret,
69
73
  redirectUri: redirect_uri
70
74
  });
71
75
  if (!response.ok) {
@@ -75,13 +79,17 @@ function createGoogleProvider() {
75
79
  const token = await response.json();
76
80
  return {
77
81
  token,
78
- userInfo: await this.getUserInfo(token)
82
+ userInfo: await this.getUserInfo(token, { clientId: params.clientId })
79
83
  };
80
84
  },
81
- async getTokenInfo(id_token) {
85
+ async getTokenInfo(id_token, clientId) {
82
86
  (0, _shware_utils.invariant)(id_token, "id_token is required");
83
87
  (0, _shware_utils.invariant)(this.jwkSetUri, "jwkSetUri is required");
84
- const data = await require_oauth2_provider_common.verifyIdToken(id_token, this.jwkSetUri);
88
+ const data = await require_oauth2_provider_common.verifyIdToken(id_token, {
89
+ jwkSetUri: this.jwkSetUri,
90
+ issuer: GOOGLE_ISSUERS,
91
+ audience: clientId
92
+ });
85
93
  return {
86
94
  token: {
87
95
  id_token,
@@ -93,16 +101,7 @@ function createGoogleProvider() {
93
101
  },
94
102
  userInfo: {
95
103
  data,
96
- claims: {
97
- sub: data.sub,
98
- name: data.name,
99
- picture: data.picture,
100
- email: data.email,
101
- email_verified: data.email_verified,
102
- given_name: data.given_name,
103
- family_name: data.family_name,
104
- locale: data.locale
105
- }
104
+ claims: claims(data)
106
105
  }
107
106
  };
108
107
  }
@@ -1 +1 @@
1
- {"version":3,"file":"google.cjs","names":["createAuthorizationUri","exchangeAuthorizationCode","OAuth2Error","verifyIdToken"],"sources":["../../../src/oauth2/provider/google.ts"],"sourcesContent":["import { invariant } from '@shware/utils';\nimport { OAuth2Error } from '../error';\nimport type {\n LoginOAuth2NativeParams,\n NativeCredential,\n OAuth2Token,\n OneTapProvider,\n} from '../types';\nimport { createAuthorizationUri, exchangeAuthorizationCode, verifyIdToken } from './common';\n\n/** The Google Cloud project number every client id of a project starts with: `<number>-<hash>.apps.googleusercontent.com`. */\nfunction projectNumber(clientId: string) {\n return clientId.split('-', 1)[0];\n}\n\nexport function createGoogleProvider(): OneTapProvider {\n return {\n authorizationUri: 'https://accounts.google.com/o/oauth2/v2/auth',\n tokenUri: 'https://oauth2.googleapis.com/token',\n userInfoUri: 'https://www.googleapis.com/oauth2/v3/userinfo',\n userNameAttribute: 'sub',\n jwkSetUri: 'https://www.googleapis.com/oauth2/v3/certs',\n // https://developers.google.com/identity/protocols/oauth2/scopes#oauth2\n defaultScope: [\n 'https://www.googleapis.com/auth/userinfo.email',\n 'https://www.googleapis.com/auth/userinfo.profile',\n 'openid',\n ],\n createAuthorizationUri(params) {\n return createAuthorizationUri({\n ...params,\n scope: params.scope ?? this.defaultScope,\n authorizationUri: this.authorizationUri,\n });\n },\n async exchangeAuthorizationCode(params) {\n const response = await exchangeAuthorizationCode({ ...params, tokenUri: this.tokenUri });\n if (!response.ok) {\n const { error, error_description } = (await response.json()) as GoogleErrorResponse;\n throw new OAuth2Error(response.status, error, error_description);\n }\n return (await response.json()) as GoogleToken;\n },\n async getUserInfo({ id_token }) {\n invariant(id_token, 'id_token is required');\n invariant(this.jwkSetUri, 'jwkSetUri is required');\n const data = await verifyIdToken<GoogleUserInfo>(id_token, this.jwkSetUri);\n return {\n data,\n claims: {\n sub: data.sub,\n name: data.name,\n picture: data.picture,\n email: data.email,\n email_verified: data.email_verified,\n given_name: data.given_name,\n family_name: data.family_name,\n locale: data.locale,\n },\n };\n },\n async loginOAuth2Native({ pkce: _, credentials, ...params }: LoginOAuth2NativeParams) {\n invariant(credentials.code, 'code is required');\n const { code } = credentials;\n const { tokenUri } = this;\n // An app signs in as its own installed-app client (the iOS / Android client id, custom-scheme\n // redirect), and Google only exchanges a code for the client it was issued to, so the exchange\n // uses the client the app names rather than the registration's web client. Installed-app\n // clients have no secret; the registration's is sent only when the exchange is for that very\n // client. Every client id carries its project number as prefix, which pins the app's client to\n // the registration's Google Cloud project — codes from a foreign project's client are refused.\n const { client_id = params.clientId, redirect_uri = params.redirectUri } = credentials;\n if (projectNumber(client_id) !== projectNumber(params.clientId)) {\n throw new OAuth2Error(400, 'invalid_client', 'client_id belongs to another project');\n }\n const response = await exchangeAuthorizationCode({\n code,\n tokenUri,\n clientId: client_id,\n clientSecret: client_id === params.clientId ? params.clientSecret : '',\n redirectUri: redirect_uri,\n });\n if (!response.ok) {\n const { error, error_description } = (await response.json()) as GoogleErrorResponse;\n throw new OAuth2Error(response.status, error, error_description);\n }\n const token = (await response.json()) as GoogleToken;\n const userInfo = await this.getUserInfo(token);\n return { token, userInfo };\n },\n async getTokenInfo(id_token: string) {\n invariant(id_token, 'id_token is required');\n invariant(this.jwkSetUri, 'jwkSetUri is required');\n const data = await verifyIdToken<GoogleUserInfo>(id_token, this.jwkSetUri);\n return {\n token: {\n id_token,\n access_token: '',\n token_type: '',\n expires_in: 0,\n refresh_token: '',\n scope: '',\n },\n userInfo: {\n data,\n claims: {\n sub: data.sub,\n name: data.name,\n picture: data.picture,\n email: data.email,\n email_verified: data.email_verified,\n given_name: data.given_name,\n family_name: data.family_name,\n locale: data.locale,\n },\n },\n };\n },\n };\n}\n\nexport const google = createGoogleProvider();\n\nexport interface GoogleUserInfo {\n aud: string;\n azp: string;\n email: string;\n email_verified: boolean;\n exp: number;\n family_name: string;\n given_name: string;\n hd?: string;\n iat: number;\n iss: string;\n jti?: string;\n locale?: string;\n name: string;\n nbf?: number;\n picture: string;\n sub: string;\n nonce?: string;\n}\n\nexport interface GoogleToken extends OAuth2Token {\n access_token: string;\n token_type: string;\n expires_in: number;\n refresh_token: string;\n id_token: string;\n scope: string;\n}\n\n// android,ios has different client_id and redirect_uri\nexport interface GoogleAppCredential extends NativeCredential {\n state: string;\n code: string;\n client_id: string;\n redirect_uri: string;\n}\n\nexport interface GoogleErrorResponse {\n error:\n | 'invalid_request'\n | 'invalid_client'\n | 'invalid_grant'\n | 'invalid_token'\n | 'invalid_scope'\n | 'unauthorized_client'\n | 'unsupported_grant_type'\n | 'access_denied'\n | 'server_error';\n error_description: string;\n}\n"],"mappings":";;;;;;AAWA,SAAS,cAAc,UAAkB;CACvC,OAAO,SAAS,MAAM,KAAK,CAAC,CAAC,CAAC;AAChC;AAEA,SAAgB,uBAAuC;CACrD,OAAO;EACL,kBAAkB;EAClB,UAAU;EACV,aAAa;EACb,mBAAmB;EACnB,WAAW;EAEX,cAAc;GACZ;GACA;GACA;EACF;EACA,uBAAuB,QAAQ;GAC7B,OAAOA,+BAAAA,uBAAuB;IAC5B,GAAG;IACH,OAAO,OAAO,SAAS,KAAK;IAC5B,kBAAkB,KAAK;GACzB,CAAC;EACH;EACA,MAAM,0BAA0B,QAAQ;GACtC,MAA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AO,iBAAiB;GACjE;GACA,MAAM,QAAS,MAAM,SAAS,KAAK;GAEnC,OAAO;IAAE;IAAO,UAAA,MADO,KAAK,YAAY,KAAK;GACpB;EAC3B;EACA,MAAM,aAAa,UAAkB;GACnC,CAAA,GAAA,cAAA,UAAA,CAAU,UAAU,sBAAsB;GAC1C,CAAA,GAAA,cAAA,UAAA,CAAU,KAAK,WAAW,uBAAuB;GACjD,MAAM,OAAO,MAAMC,+BAAAA,cAA8B,UAAU,KAAK,SAAS;GACzE,OAAO;IACL,OAAO;KACL;KACA,cAAc;KACd,YAAY;KACZ,YAAY;KACZ,eAAe;KACf,OAAO;IACT;IACA,UAAU;KACR;KACA,QAAQ;MACN,KAAK,KAAK;MACV,MAAM,KAAK;MACX,SAAS,KAAK;MACd,OAAO,KAAK;MACZ,gBAAgB,KAAK;MACrB,YAAY,KAAK;MACjB,aAAa,KAAK;MAClB,QAAQ,KAAK;KACf;IACF;GACF;EACF;CACF;AACF;AAEA,MAAa,SAAS,qBAAqB"}
1
+ {"version":3,"file":"google.cjs","names":["createAuthorizationUri","exchangeAuthorizationCode","OAuth2Error","verifyIdToken"],"sources":["../../../src/oauth2/provider/google.ts"],"sourcesContent":["import { invariant } from '@shware/utils';\nimport { OAuth2Error } from '../error';\nimport type {\n LoginOAuth2NativeParams,\n NativeCredential,\n OAuth2Token,\n OneTapProvider,\n} from '../types';\nimport { createAuthorizationUri, exchangeAuthorizationCode, verifyIdToken } from './common';\n\n/** Google signs id tokens with either `iss`, the URL or the bare host. */\nconst GOOGLE_ISSUERS = ['https://accounts.google.com', 'accounts.google.com'];\n\nfunction claims(data: GoogleUserInfo) {\n return {\n sub: data.sub,\n name: data.name,\n picture: data.picture,\n email: data.email,\n email_verified: data.email_verified,\n given_name: data.given_name,\n family_name: data.family_name,\n locale: data.locale,\n };\n}\n\nexport function createGoogleProvider(): OneTapProvider {\n return {\n authorizationUri: 'https://accounts.google.com/o/oauth2/v2/auth',\n tokenUri: 'https://oauth2.googleapis.com/token',\n userInfoUri: 'https://www.googleapis.com/oauth2/v3/userinfo',\n userNameAttribute: 'sub',\n jwkSetUri: 'https://www.googleapis.com/oauth2/v3/certs',\n // https://developers.google.com/identity/protocols/oauth2/scopes#oauth2\n defaultScope: [\n 'https://www.googleapis.com/auth/userinfo.email',\n 'https://www.googleapis.com/auth/userinfo.profile',\n 'openid',\n ],\n createAuthorizationUri(params) {\n return createAuthorizationUri({\n ...params,\n scope: params.scope ?? this.defaultScope,\n authorizationUri: this.authorizationUri,\n });\n },\n async exchangeAuthorizationCode(params) {\n const response = await exchangeAuthorizationCode({ ...params, tokenUri: this.tokenUri });\n if (!response.ok) {\n const { error, error_description } = (await response.json()) as GoogleErrorResponse;\n throw new OAuth2Error(response.status, error, error_description);\n }\n return (await response.json()) as GoogleToken;\n },\n async getUserInfo({ id_token }, { clientId }) {\n invariant(id_token, 'id_token is required');\n invariant(this.jwkSetUri, 'jwkSetUri is required');\n const data = await verifyIdToken<GoogleUserInfo>(id_token, {\n jwkSetUri: this.jwkSetUri,\n issuer: GOOGLE_ISSUERS,\n audience: clientId,\n });\n return { data, claims: claims(data) };\n },\n async loginOAuth2Native({ pkce: _, credentials, ...params }: LoginOAuth2NativeParams) {\n invariant(credentials.code, 'code is required');\n const { code } = credentials;\n const { tokenUri } = this;\n // An app signs in as its own installed-app client (the iOS / Android client id, custom-scheme\n // redirect), and Google only exchanges a code for the client it was issued to, so the exchange\n // runs as the client the app names, which the OAuth2 client has already checked is one of this\n // provider's configured clients. Installed-app clients have no secret. The redirect URI is the\n // one the app used, which only the app knows.\n const { redirect_uri = params.redirectUri } = credentials;\n const response = await exchangeAuthorizationCode({\n code,\n tokenUri,\n clientId: params.clientId,\n clientSecret: params.clientSecret,\n redirectUri: redirect_uri,\n });\n if (!response.ok) {\n const { error, error_description } = (await response.json()) as GoogleErrorResponse;\n throw new OAuth2Error(response.status, error, error_description);\n }\n const token = (await response.json()) as GoogleToken;\n const userInfo = await this.getUserInfo(token, { clientId: params.clientId });\n return { token, userInfo };\n },\n async getTokenInfo(id_token: string, clientId: string) {\n invariant(id_token, 'id_token is required');\n invariant(this.jwkSetUri, 'jwkSetUri is required');\n // One Tap and FedCM issue the credential to the web client.\n const data = await verifyIdToken<GoogleUserInfo>(id_token, {\n jwkSetUri: this.jwkSetUri,\n issuer: GOOGLE_ISSUERS,\n audience: clientId,\n });\n return {\n token: {\n id_token,\n access_token: '',\n token_type: '',\n expires_in: 0,\n refresh_token: '',\n scope: '',\n },\n userInfo: { data, claims: claims(data) },\n };\n },\n };\n}\n\nexport const google = createGoogleProvider();\n\nexport interface GoogleUserInfo {\n aud: string;\n azp: string;\n email: string;\n email_verified: boolean;\n exp: number;\n family_name: string;\n given_name: string;\n hd?: string;\n iat: number;\n iss: string;\n jti?: string;\n locale?: string;\n name: string;\n nbf?: number;\n picture: string;\n sub: string;\n nonce?: string;\n}\n\nexport interface GoogleToken extends OAuth2Token {\n access_token: string;\n token_type: string;\n expires_in: number;\n refresh_token: string;\n id_token: string;\n scope: string;\n}\n\n// android,ios has different client_id and redirect_uri\nexport interface GoogleAppCredential extends NativeCredential {\n state: string;\n code: string;\n client_id: string;\n redirect_uri: string;\n}\n\nexport interface GoogleErrorResponse {\n error:\n | 'invalid_request'\n | 'invalid_client'\n | 'invalid_grant'\n | 'invalid_token'\n | 'invalid_scope'\n | 'unauthorized_client'\n | 'unsupported_grant_type'\n | 'access_denied'\n | 'server_error';\n error_description: string;\n}\n"],"mappings":";;;;;;AAWA,MAAM,iBAAiB,CAAC,+BAA+B,qBAAqB;AAE5E,SAAS,OAAO,MAAsB;CACpC,OAAO;EACL,KAAK,KAAK;EACV,MAAM,KAAK;EACX,SAAS,KAAK;EACd,OAAO,KAAK;EACZ,gBAAgB,KAAK;EACrB,YAAY,KAAK;EACjB,aAAa,KAAK;EAClB,QAAQ,KAAK;CACf;AACF;AAEA,SAAgB,uBAAuC;CACrD,OAAO;EACL,kBAAkB;EAClB,UAAU;EACV,aAAa;EACb,mBAAm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iBAAiB;GACjE;GACA,MAAM,QAAS,MAAM,SAAS,KAAK;GAEnC,OAAO;IAAE;IAAO,UAAA,MADO,KAAK,YAAY,OAAO,EAAE,UAAU,OAAO,SAAS,CAAC;GACnD;EAC3B;EACA,MAAM,aAAa,UAAkB,UAAkB;GACrD,CAAA,GAAA,cAAA,UAAA,CAAU,UAAU,sBAAsB;GAC1C,CAAA,GAAA,cAAA,UAAA,CAAU,KAAK,WAAW,uBAAuB;GAEjD,MAAM,OAAO,MAAMC,+BAAAA,cAA8B,UAAU;IACzD,WAAW,KAAK;IAChB,QAAQ;IACR,UAAU;GACZ,CAAC;GACD,OAAO;IACL,OAAO;KACL;KACA,cAAc;KACd,YAAY;KACZ,YAAY;KACZ,eAAe;KACf,OAAO;IACT;IACA,UAAU;KAAE;KAAM,QAAQ,OAAO,IAAI;IAAE;GACzC;EACF;CACF;AACF;AAEA,MAAa,SAAS,qBAAqB"}
@@ -1 +1 @@
1
- {"version":3,"file":"google.d.cts","names":[],"sources":["../../../src/oauth2/provider/google.ts"],"mappings":";;wBAegB,wBAAwB;qBA0G3B,QAAM;iBAEF;EACf;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;;iBAGe,oBAAoB;EACnC;EACA;EACA;EACA;EACA;EACA;;iBAIe,4BAA4B;EAC3C;EACA;EACA;EACA;;iBAGe;EACf;EAUA"}
1
+ {"version":3,"file":"google.d.cts","names":[],"sources":["../../../src/oauth2/provider/google.ts"],"mappings":";;wBA0BgB,wBAAwB;qBAuF3B,QAAM;iBAEF;EACf;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;;iBAGe,oBAAoB;EACnC;EACA;EACA;EACA;EACA;EACA;;iBAIe,4BAA4B;EAC3C;EACA;EACA;EACA;;iBAGe;EACf;EAUA"}
@@ -1 +1 @@
1
- {"version":3,"file":"google.d.mts","names":[],"sources":["../../../src/oauth2/provider/google.ts"],"mappings":";;wBAegB,wBAAwB;qBA0G3B,QAAM;iBAEF;EACf;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;;iBAGe,oBAAoB;EACnC;EACA;EACA;EACA;EACA;EACA;;iBAIe,4BAA4B;EAC3C;EACA;EACA;EACA;;iBAGe;EACf;EAUA"}
1
+ {"version":3,"file":"google.d.mts","names":[],"sources":["../../../src/oauth2/provider/google.ts"],"mappings":";;wBA0BgB,wBAAwB;qBAuF3B,QAAM;iBAEF;EACf;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;EACA;;iBAGe,oBAAoB;EACnC;EACA;EACA;EACA;EACA;EACA;;iBAIe,4BAA4B;EAC3C;EACA;EACA;EACA;;iBAGe;EACf;EAUA"}