@randajan/oauth2-client
Version:
Lightweight Node.js helper that streamlines OAuth 2.0 and service-account authentication for all Google APIs, giving downstream packages hassle-free token acquisition and refresh
8 lines (7 loc) • 5.06 kB
Source Map (JSON)
{
"version": 3,
"sources": ["../../../src/google/GoogleAccount.js", "../../../src/google/GoogleGrant.js", "../../../src/google/index.js"],
"sourcesContent": ["import { google } from \"googleapis\";\r\nimport { solids } from \"@randajan/props\";\r\n\r\nimport { OAuth2Account } from \"../class/OAuth2Account\";\r\nimport { vault } from \"../consts\";\r\n\r\n\r\n\r\nexport class GoogleAccount extends OAuth2Account {\r\n\r\n constructor(client, credentials={}) {\r\n super(client, credentials);\r\n\r\n const grant = vault.get(client);\r\n\r\n const auth = grant.createAuth();\r\n auth.setCredentials(this.credentials);\r\n auth.on('tokens', _=>{ grant.onRenew(this); });\r\n\r\n solids(this, {\r\n auth,\r\n });\r\n\r\n }\r\n\r\n oauth2() {\r\n return google.oauth2({ auth: this.auth, version: 'v2' });\r\n }\r\n\r\n async profile() {\r\n const { data } = await this.oauth2().userinfo.get();\r\n return data;\r\n }\r\n\r\n async tokens() {\r\n const { token } = await this.auth.getAccessToken();\r\n return { ...this.auth.credentials, access_token: token };\r\n }\r\n\r\n async scopes() {\r\n const { auth } = this;\r\n \r\n if (auth.credentials?.scope) { return super.scopes(auth.credentials.scope); }\r\n\r\n const { token } = await auth.getAccessToken();\r\n if (!token) { return []; }\r\n\r\n const info = await auth.getTokenInfo(token);\r\n if (!info) { return []; }\r\n\r\n return super.scopes(info.scopes);\r\n }\r\n\r\n}", "import { google } from \"googleapis\";\r\nimport { OAuth2Grant } from \"../class/OAuth2Grant\";\r\nimport { GoogleAccount } from \"./GoogleAccount\";\r\n\r\nexport class GoogleGrant extends OAuth2Grant {\r\n\r\n static name=\"google\";\r\n static uidKey=\"id\";\r\n static scopePrefix=\"https://www.googleapis.com/auth/\";\r\n static scopesCommon=[\"openid\", \"userinfo.profile\", \"userinfo.email\"];\r\n static scopesNoPrefix=[\"openid\"];\r\n static Account = GoogleAccount;\r\n\r\n constructor(client, opt={}) {\r\n super(client, opt);\r\n\r\n const { optExtra, clientId, clientSecret, redirectUri } = this;\r\n\r\n this.optAuth = { ...optExtra, clientId, clientSecret, redirectUri };\r\n this.auth = this.createAuth();\r\n }\r\n\r\n createAuth() {\r\n return new google.auth.OAuth2(this.optAuth);\r\n }\r\n\r\n generateAuthUrl(scope, state, extra) {\r\n return this.auth.generateAuthUrl({\r\n ...extra,\r\n access_type: this.isOffline ? \"offline\" : \"online\",\r\n scope,\r\n state\r\n });\r\n }\r\n\r\n async swapCodeForTokens(code) {\r\n const { tokens } = await this.auth.getToken(code);\r\n return tokens;\r\n }\r\n\r\n}", "import { OAuth2Client } from \"../class/OAuth2Client\";\r\nimport { GoogleAccount } from \"./GoogleAccount\";\r\nimport { GoogleGrant } from \"./GoogleGrant\";\r\n\r\n\r\nexport const createGoogleOAuth2 = (options={})=>new OAuth2Client(GoogleGrant, options);\r\nexport default createGoogleOAuth2;\r\n\r\nexport {\r\n GoogleAccount,\r\n GoogleGrant\r\n}"],
"mappings": ";;;;;;;;;AAAA,SAAS,cAAc;AACvB,SAAS,cAAc;AAOhB,IAAM,gBAAN,cAA4B,cAAc;AAAA,EAE7C,YAAY,QAAQ,cAAY,CAAC,GAAG;AAChC,UAAM,QAAQ,WAAW;AAEzB,UAAM,QAAQ,MAAM,IAAI,MAAM;AAE9B,UAAM,OAAO,MAAM,WAAW;AAC9B,SAAK,eAAe,KAAK,WAAW;AACpC,SAAK,GAAG,UAAU,OAAG;AAAE,YAAM,QAAQ,IAAI;AAAA,IAAG,CAAC;AAE7C,WAAO,MAAM;AAAA,MACT;AAAA,IACJ,CAAC;AAAA,EAEL;AAAA,EAEA,SAAS;AACL,WAAO,OAAO,OAAO,EAAE,MAAM,KAAK,MAAM,SAAS,KAAK,CAAC;AAAA,EAC3D;AAAA,EAEA,MAAM,UAAU;AACZ,UAAM,EAAE,KAAK,IAAI,MAAM,KAAK,OAAO,EAAE,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