Created
May 18, 2020 16:01
-
-
Save rhyskentish/0ad67e9e1af62d108e0f32a77e85919a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const projectId = <YourProjectID>; | |
| const locationId = <YourPreferedStorageLocationID>; //Based on the google cloud regions e.g. us-central1 | |
| const keyRingId = <KeyRingID>; | |
| const keyId = <YourKeyId>; | |
| const kmsClient = new KeyManagementServiceClient(); | |
| const locationName = kmsClient.locationPath(projectId, locationId); | |
| async function encryptAndStoreToken(token: any) { | |
| const keyName = kmsClient.cryptoKeyPath(projectId, locationId, keyRingId, keyId); | |
| const [encryptedAccessTokenResponse] = await kmsClient.encrypt({ | |
| name: keyName, | |
| plaintext: Buffer.from(token.access_token) | |
| }); | |
| const [encryptedRefreshTokenResponse] = await kmsClient.encrypt({ | |
| name: keyName, | |
| plaintext: Buffer.from(token.refresh_token) | |
| }); | |
| const encryptedTokens = { | |
| access_token: encryptedAccessTokenResponse.ciphertext, | |
| expiry_date: token.expiry_date, | |
| refresh_token: encryptedRefreshTokenResponse.ciphertext, | |
| scope: token.scope, | |
| token_type: token.token_type | |
| }; | |
| const data = { | |
| api_tokens: encryptedTokens | |
| }; | |
| const teamRef = admin.firestore().collection(collection).doc(<YourDocId>); | |
| teamRef.set(data, {merge: true}) | |
| .then(() => { | |
| return | |
| }) | |
| .catch(err => { | |
| console.log('Error setting document', err); | |
| return | |
| }); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment