cloudflare-workers-and-google-oauth
Advanced tools
Comparing version 1.0.5 to 1.0.6
@@ -14,7 +14,11 @@ export interface GoogleKey { | ||
export default class GoogleOAuth { | ||
objectToBase64url: (object: object) => string; | ||
googleKey: GoogleKey; | ||
scopes: string[]; | ||
constructor(googleKey: GoogleKey, scopes: string[]); | ||
getGoogleAuthToken(): Promise<string | undefined>; | ||
private objectToBase64url; | ||
private arrayBufferToBase64Url; | ||
private str2ab; | ||
private sign; | ||
getGoogleAuthToken(user: string, key: string, scope: string): Promise<string | undefined>; | ||
private formatScopes; | ||
} |
@@ -15,40 +15,15 @@ "use strict"; | ||
// Inspiration: https://gist.github.com/markelliot/6627143be1fc8209c9662c504d0ff205 | ||
// | ||
// GoogleOAuth encapsulates the logic required to retrieve an access token | ||
// for the OAuth flow. | ||
class GoogleOAuth { | ||
constructor() { | ||
this.objectToBase64url = (object) => this.arrayBufferToBase64Url(new TextEncoder().encode(JSON.stringify(object))); | ||
//TODO: Probably doesn't need to have ctor at all, but leaving it | ||
constructor(googleKey, scopes) { | ||
this.googleKey = googleKey; | ||
this.scopes = scopes; | ||
} | ||
arrayBufferToBase64Url(buffer) { | ||
return btoa(String.fromCharCode(...new Uint8Array(buffer))) | ||
.replace(/=/g, '') | ||
.replace(/\+/g, '-') | ||
.replace(/\//g, '_'); | ||
} | ||
str2ab(str) { | ||
const buf = new ArrayBuffer(str.length); | ||
const bufView = new Uint8Array(buf); | ||
for (let i = 0, strLen = str.length; i < strLen; i += 1) { | ||
bufView[i] = str.charCodeAt(i); | ||
} | ||
return buf; | ||
} | ||
sign(content, signingKey) { | ||
getGoogleAuthToken() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const buf = this.str2ab(content); | ||
const plainKey = signingKey | ||
.replace(PEM_HEADER, '') | ||
.replace(PEM_FOOTER, '') | ||
.replace(/(\r\n|\n|\r)/gm, ''); | ||
const binaryKey = this.str2ab(atob(plainKey)); | ||
const signer = yield crypto.subtle.importKey('pkcs8', binaryKey, { | ||
name: 'RSASSA-PKCS1-V1_5', | ||
hash: { name: 'SHA-256' }, | ||
}, false, ['sign']); | ||
const binarySignature = yield crypto.subtle.sign({ name: 'RSASSA-PKCS1-V1_5' }, signer, buf); | ||
return this.arrayBufferToBase64Url(binarySignature); | ||
}); | ||
} | ||
getGoogleAuthToken(user, key, scope) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const { client_email: user, private_key: key } = this.googleKey; | ||
const scope = this.formatScopes(this.scopes); | ||
const jwtHeader = this.objectToBase64url({ alg: 'RS256', typ: 'JWT' }); | ||
@@ -86,3 +61,40 @@ try { | ||
} | ||
objectToBase64url(object) { | ||
return this.arrayBufferToBase64Url(new TextEncoder().encode(JSON.stringify(object))); | ||
} | ||
arrayBufferToBase64Url(buffer) { | ||
return btoa(String.fromCharCode(...new Uint8Array(buffer))) | ||
.replace(/=/g, '') | ||
.replace(/\+/g, '-') | ||
.replace(/\//g, '_'); | ||
} | ||
str2ab(str) { | ||
const buf = new ArrayBuffer(str.length); | ||
const bufView = new Uint8Array(buf); | ||
for (let i = 0, strLen = str.length; i < strLen; i += 1) { | ||
bufView[i] = str.charCodeAt(i); | ||
} | ||
return buf; | ||
} | ||
sign(content, signingKey) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const buf = this.str2ab(content); | ||
const plainKey = signingKey | ||
.replace(PEM_HEADER, '') | ||
.replace(PEM_FOOTER, '') | ||
.replace(/(\r\n|\n|\r)/gm, ''); | ||
const binaryKey = this.str2ab(atob(plainKey)); | ||
const signer = yield crypto.subtle.importKey('pkcs8', binaryKey, { | ||
name: 'RSASSA-PKCS1-V1_5', | ||
hash: { name: 'SHA-256' }, | ||
}, false, ['sign']); | ||
const binarySignature = yield crypto.subtle.sign({ name: 'RSASSA-PKCS1-V1_5' }, signer, buf); | ||
return this.arrayBufferToBase64Url(binarySignature); | ||
}); | ||
} | ||
// formatScopes will create a scopes string that is formatted for the Google API | ||
formatScopes(scopes) { | ||
return scopes.join(' '); | ||
} | ||
} | ||
exports.default = GoogleOAuth; |
{ | ||
"name": "cloudflare-workers-and-google-oauth", | ||
"version": "1.0.5", | ||
"version": "1.0.6", | ||
"description": "Enables easier interfacing with GCS OAuth API for use in Cloudflare Workers", | ||
@@ -5,0 +5,0 @@ "main": "dist/index.js", |
@@ -5,2 +5,21 @@ # Google OAuth & Cloudflare Workers | ||
This is a simple JS module used to facilitate OAuth2 flow from within a Cloudflare Worker. This module isn't specific to Cloudflare, but it is compatible within isolates and doesn't rely on NodeJS. | ||
This is a simple JS module used to facilitate OAuth2 flow from within a Cloudflare Worker. This module isn't specific to Cloudflare, but it is compatible within isolates and doesn't rely on NodeJS. | ||
## Usage | ||
Install: `npm i cloudflare-workers-and-google-oauth` | ||
``` | ||
// import the lib | ||
import GoogleAuth, { GoogleKey } from 'cloudflare-workers-and-google-oauth' | ||
// ensure you have global access to the environment variable representing the PEM-encoded secret | ||
// you downloaded from Google Cloud dashboard for your service account | ||
export interface Env { | ||
GCP_SERVICE_ACCOUNT: string; | ||
} | ||
... | ||
``` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
6483
121
25