Skip to content

Commit

Permalink
Adds the ability to add 'scopes' to a gaia auth token
Browse files Browse the repository at this point in the history
  • Loading branch information
hstove committed Oct 24, 2018
1 parent 0e78c52 commit 77c6555
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions src/storage/hub.js
Expand Up @@ -18,6 +18,11 @@ export type GaiaHubConfig = {
server: string
}

type AuthScopeType = {
scope: string,
domain: string
}

export function uploadToGaiaHub(filename: string, contents: any,
hubConfig: GaiaHubConfig,
contentType: string = 'application/octet-stream'): Promise<*> {
Expand Down Expand Up @@ -68,7 +73,8 @@ function makeLegacyAuthToken(challengeText: string, signerKeyHex: string): strin
function makeV1GaiaAuthToken(hubInfo: Object,
signerKeyHex: string,
hubUrl: string,
associationToken?: string): string {
associationToken?: string,
scopes?: Array<AuthScopeType>): string {
const challengeText = hubInfo.challenge_text
const handlesV1Auth = (hubInfo.latest_auth_version
&& parseInt(hubInfo.latest_auth_version.slice(1), 10) >= 1)
Expand All @@ -84,15 +90,17 @@ function makeV1GaiaAuthToken(hubInfo: Object,
hubUrl,
iss,
salt,
associationToken
associationToken,
scopes
}
const token = new TokenSigner('ES256K', signerKeyHex).sign(payload)
return `v1:${token}`
}

export function connectToGaiaHub(gaiaHubUrl: string,
challengeSignerHex: string,
associationToken?: string): Promise<GaiaHubConfig> {
associationToken?: string,
scopes?: Array<AuthScopeType>): Promise<GaiaHubConfig> {
if (!associationToken) {
// maybe given in local storage?
try {
Expand All @@ -111,7 +119,7 @@ export function connectToGaiaHub(gaiaHubUrl: string,
.then(response => response.json())
.then((hubInfo) => {
const readURL = hubInfo.read_url_prefix
const token = makeV1GaiaAuthToken(hubInfo, challengeSignerHex, gaiaHubUrl, associationToken)
const token = makeV1GaiaAuthToken(hubInfo, challengeSignerHex, gaiaHubUrl, associationToken, scopes)
const address = ecPairToAddress(hexStringToECPair(challengeSignerHex
+ (challengeSignerHex.length === 64 ? '01' : '')))
return {
Expand Down

0 comments on commit 77c6555

Please sign in to comment.