# All files / src/auth authSession.js

0% Statements 0/43
0% Branches 0/21
0% Functions 0/8
0% Lines 0/43

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
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164

     /* @flow */
    import { TokenSigner, decodeToken, SECP256K1Client } from 'jsontokens'
    import 'cross-fetch/polyfill'

    /**
     * Create an authentication token to be sent to the Core API server
     * in order to generate a Core session JWT.
     *
     * @param {String} appDomain  The unique application identifier (e.g. foo.app, www.foo.com, etc).
     * @param {Array} appMethods  The list of API methods this application will need.
     * @param {String} appPrivateKey  The application-specific private key
     * @param {String|null} blockchainID  This is the blockchain ID of the requester
     * @param {String} thisDevice Identifier of the current device
     *
     * @return {String} a JWT signed by the app's private key
     * @deprecated
     * @private
     */
    export function makeCoreSessionRequest(appDomain: string,
                                           appMethods: Array<string>,
                                           appPrivateKey: string,
                                           blockchainID: ?string = null,
                                           thisDevice: ?string = null) {
      if (thisDevice === null) {
        thisDevice = '.default'
      }

      const appPublicKey = SECP256K1Client.derivePublicKey(appPrivateKey)
      const appPublicKeys = [{
        public_key: appPublicKey,
        device_id: thisDevice
      }]

      const authBody = {
        version: 1,
        blockchain_id: blockchainID,
        app_private_key: appPrivateKey,
        app_domain: appDomain,
        methods: appMethods,
        app_public_keys: appPublicKeys,
        device_id: thisDevice
      }

      // make token
      const tokenSigner = new TokenSigner('ES256k', appPrivateKey)
      const token = tokenSigner.sign(authBody)

      return token
    }

    /**
     * Send Core a request for a session token.
     *
     * @param {String} coreHost host name of the core node
     * @param {Number} corePort port number of the core node
     * @param {String} coreAuthRequest  a signed JWT encoding the authentication request
     * @param {String} apiPassword the API password for Core
     *
     * @return {Promise} the resolves to a JWT signed with the Core API server's private key
     * that authorizes the bearer to carry out the requested operations and rejects
     * with an error message otherwise
     * @deprecated
     * @private
     */
    export function sendCoreSessionRequest(coreHost: string,
                                           corePort: number,
                                           coreAuthRequest: string,
                                           apiPassword: string) {
      return Promise.resolve().then(() => {
        if (!apiPassword) {
          throw new Error('Missing API password')
        }
      })
        .then(() => {
          const options = {
            headers: {
              Authorization: `bearer ${apiPassword}`
            }
          }
          const url = `http://${coreHost}:${corePort}/v1/auth?authRequest=${coreAuthRequest}`
          return fetch(url, options)
        })
        .then((response) => {
          if (!response.ok) {
            throw new Error('HTTP status not OK')
          }
          return response.text()
        })
        .then((responseText) => {
          const responseJson = JSON.parse(responseText)
          const token = responseJson.token
          if (!token) {
            throw new Error('Failed to get Core session token')
          }
          return token
        })
        .catch((error) => {
          console.error(error)
          throw new Error('Invalid Core response: not JSON')
        })
    }

    /**
     * Get a core session token.  Generate an auth request, sign it, send it to Core,
     * and get back a session token.
     *
     * @param {String} coreHost Core API server's hostname
     * @param {Number} corePort Core API server's port number
     * @param {String} apiPassword core api password
     * @param  {String} appPrivateKey Application's private key
     * @param  {String} blockchainId blockchain ID of the user signing in.
     * `null` if user has no blockchain ID
     * @param {String} authRequest authentication request token
     * @param {String} deviceId identifier for the current device
     *
     * @return {Promise} a Promise that resolves to a Core session token or rejects
     * with an error message.
     * @deprecated
     * @private
     */
    export function getCoreSession(coreHost: string,
                                   corePort: number,
                                   apiPassword: string,
                                   appPrivateKey: string,
                                   blockchainId: ?string = null,
                                   authRequest: ?string = null,
                                   deviceId: string = '0') {
      if (!authRequest) {
        return Promise.reject('No authRequest provided')
      }

      let payload = null
      let authRequestObject = null
      try {
        authRequestObject = decodeToken(authRequest)
        if (!authRequestObject) {
          return Promise.reject('Invalid authRequest in URL query string')
        }
        if (!authRequestObject.payload) {
          return Promise.reject('Invalid authRequest in URL query string')
        }
        payload = authRequestObject.payload
      } catch (e) {
        console.error(e.stack)
        return Promise.reject('Failed to parse authRequest in URL')
      }

      const appDomain = payload.domain_name
      if (!appDomain) {
        return Promise.reject('No domain_name in authRequest')
      }
      const appMethods = payload.scopes

      const coreAuthRequest = makeCoreSessionRequest(
        appDomain, appMethods, appPrivateKey, blockchainId, deviceId
      )

      return sendCoreSessionRequest(
        coreHost, corePort, coreAuthRequest, apiPassword
      )
    }