All files / src/auth authProvider.js

38.46% Statements 10/26
12.5% Branches 1/8
62.5% Functions 5/8
38.46% Lines 10/26

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81                                                                1x 1x     1x 1x 1x 1x 1x 1x 1x   1x                                                                        
import queryString from 'query-string'
import { decodeToken } from 'jsontokens'
import { updateQueryStringParameter } from '../index'
import { BLOCKSTACK_HANDLER } from '../utils'
 
import { Logger } from '../logger'
 
/**
 * Retrieves the authentication request from the query string
 * @return {String|null} the authentication request or `null` if
 * the query string parameter `authRequest` is not found
 * @private
 */
export function getAuthRequestFromURL() {
  const queryDict = queryString.parse(location.search)
  if (queryDict.authRequest !== null && queryDict.authRequest !== undefined) {
    return queryDict.authRequest.split(`${BLOCKSTACK_HANDLER}:`).join('')
  } else {
    return null
  }
}
 
/**
 * Fetches the contents of the manifest file specified in the authentication request
 *
 * @param  {String} authRequest encoded and signed authentication request
 * @return {Promise<Object|String>} Returns a `Promise` that resolves to the JSON
 * object manifest file unless there's an error in which case rejects with an error
 * message.
 * @private
 */
export function fetchAppManifest(authRequest) {
  return new Promise((resolve, reject) => {
    Iif (!authRequest) {
      reject('Invalid auth request')
    } else {
      const payload = decodeToken(authRequest).payload
      const manifestURI = payload.manifest_uri
      try {
        Logger.debug(`Fetching manifest from ${manifestURI}`)
        fetch(manifestURI)
          .then(response => response.text())
          .then(responseText => JSON.parse(responseText))
          .then((responseJSON) => {
            resolve(responseJSON)
          })
          .catch((e) => {
            Logger.debug(e.stack)
            reject('Could not fetch manifest.json')
          })
      } catch (e) {
        Logger.debug(e.stack)
        reject('Could not fetch manifest.json')
      }
    }
  })
}
 
/**
 * Redirect the user's browser to the app using the `redirect_uri`
 * specified in the authentication request, passing the authentication
 * response token as a query parameter.
 *
 * @param {String} authRequest  encoded and signed authentication request token
 * @param  {String} authResponse encoded and signed authentication response token
 * @return {void}
 * @throws {Error} if there is no redirect uri
 * @private
 */
export function redirectUserToApp(authRequest, authResponse) {
  const payload = decodeToken(authRequest).payload
  let redirectURI = payload.redirect_uri
  Logger.debug(redirectURI)
  if (redirectURI) {
    redirectURI = updateQueryStringParameter(redirectURI, 'authResponse', authResponse)
  } else {
    throw new Error('Invalid redirect URI')
  }
  window.location = redirectURI
}