Skip to content
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

Adds the ability to add 'scopes' to a gaia auth token #545

Closed
wants to merge 6 commits into from
Closed
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 16 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,11 @@ 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