-
Notifications
You must be signed in to change notification settings - Fork 126
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add ability to encrypt session data #818
Closed
Closed
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@shopify/shopify-api": minor | ||
--- | ||
|
||
Added the ability to encrypt Session access tokens using AES-GCM with a 128-bit tag and a 12-byte random IV. |
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,20 @@ | ||
/* eslint-disable no-fallthrough */ | ||
|
||
import {InvalidSession} from '../error'; | ||
import {OnlineAccessInfo} from '../auth/oauth/types'; | ||
import {AuthScopes} from '../auth/scopes'; | ||
import { | ||
decryptString, | ||
encryptString, | ||
generateIV, | ||
asBase64, | ||
fromBase64, | ||
} from '../../runtime/crypto'; | ||
|
||
import {SessionParams} from './types'; | ||
|
||
type SessionParamsArray = [string, string | number | boolean][]; | ||
|
||
const propertiesToSave = [ | ||
'id', | ||
'shop', | ||
|
@@ -20,8 +30,10 @@ const propertiesToSave = [ | |
* Stores App information from logged in merchants so they can make authenticated requests to the Admin API. | ||
*/ | ||
export class Session { | ||
private static CIPHER_PREFIX = 'encrypted#'; | ||
|
||
public static fromPropertyArray( | ||
entries: [string, string | number | boolean][], | ||
entries: SessionParamsArray, | ||
returnUserData = false, | ||
): Session { | ||
if (!Array.isArray(entries)) { | ||
|
@@ -134,6 +146,48 @@ export class Session { | |
return session; | ||
} | ||
|
||
public static async fromEncryptedPropertyArray( | ||
entries: SessionParamsArray, | ||
cryptoKey: CryptoKey, | ||
returnUserData = false, | ||
) { | ||
const decryptedEntries: SessionParamsArray = []; | ||
for (const [key, value] of entries) { | ||
switch (key) { | ||
case 'accessToken': | ||
decryptedEntries.push([ | ||
key, | ||
await this.decryptValue(value as string, cryptoKey), | ||
]); | ||
break; | ||
default: | ||
decryptedEntries.push([key, value]); | ||
break; | ||
} | ||
} | ||
|
||
return this.fromPropertyArray(decryptedEntries, returnUserData); | ||
} | ||
|
||
private static async encryptValue(value: string, key: CryptoKey) { | ||
const iv = generateIV(); | ||
const cipher = await encryptString(value, {key, iv}); | ||
|
||
return `${Session.CIPHER_PREFIX}${asBase64(iv)}${cipher}`; | ||
} | ||
|
||
private static async decryptValue(value: string, key: CryptoKey) { | ||
if (!value.startsWith(Session.CIPHER_PREFIX)) { | ||
return value; | ||
} | ||
|
||
const keyString = value.slice(Session.CIPHER_PREFIX.length); | ||
paulomarg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const iv = new Uint8Array(fromBase64(keyString.slice(0, 16))); | ||
const cipher = keyString.slice(16); | ||
|
||
return decryptString(cipher, {key, iv}); | ||
} | ||
|
||
/** | ||
* The unique identifier for the session. | ||
*/ | ||
|
@@ -208,7 +262,7 @@ export class Session { | |
} | ||
|
||
/** | ||
* Converts an object with data into a Session. | ||
* Converts a Session into an object with its data, that can be used to construct another Session. | ||
*/ | ||
public toObject(): SessionParams { | ||
const object: SessionParams = { | ||
|
@@ -259,19 +313,42 @@ export class Session { | |
/** | ||
* Converts the session into an array of key-value pairs. | ||
*/ | ||
public toPropertyArray( | ||
public toPropertyArray(returnUserData = false): SessionParamsArray { | ||
return this.flattenProperties(this.toObject(), returnUserData); | ||
} | ||
|
||
/** | ||
* Converts the session into an array of key-value pairs, encrypting sensitive data. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could explicitly say it is the access token that is getting encrypted. |
||
* | ||
* The encrypted string will contain both the IV and the encrypted value. | ||
*/ | ||
public async toEncryptedPropertyArray( | ||
key: CryptoKey, | ||
returnUserData = false, | ||
): [string, string | number | boolean][] { | ||
): Promise<SessionParamsArray> { | ||
const object = this.toObject(); | ||
|
||
if (object.accessToken) { | ||
object.accessToken = await Session.encryptValue(object.accessToken, key); | ||
} | ||
|
||
return this.flattenProperties(object, returnUserData); | ||
} | ||
|
||
private flattenProperties( | ||
params: SessionParams, | ||
returnUserData: boolean, | ||
): SessionParamsArray { | ||
return ( | ||
Object.entries(this) | ||
Object.entries(params) | ||
.filter( | ||
([key, value]) => | ||
propertiesToSave.includes(key) && | ||
value !== undefined && | ||
value !== null, | ||
) | ||
// Prepare values for db storage | ||
.flatMap(([key, value]): [string, string | number | boolean][] => { | ||
.flatMap(([key, value]): SessionParamsArray => { | ||
switch (key) { | ||
case 'expires': | ||
return [[key, value ? value.getTime() : undefined]]; | ||
|
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
import '../crypto/__tests__/encrypt.test'; | ||
import '../crypto/__tests__/hmac.test'; | ||
import '../http/__tests__/http.test'; | ||
import '../platform/__tests__/platform.test'; |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Question: Should we be making it flexible to allow folks to encrypt more than just the accessToken?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought of that too, but haven't thought of how the app would convey that information. I think it would need to be a direct argument to
to
/fromEncryptedArray
that the app would have to pass in to the session storage.That would mean the app could do something like:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I think it would have to be something like that. I think it would be a nice to have, but does go beyond the scope of this required work.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is fairly easy to implement though, I'm going to timebox it and see if I can get it off the ground.