# All files / src/auth authSession.ts

16.22% Statements 6/37
0% Branches 0/5
0% Functions 0/8
16.22% Lines 6/37

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
    165
    166
    167
    168
    169
    1x
    1x
    1x

    1x

    1x

    1x

     // @ts-ignore: Could not find a declaration file for module
    import { TokenSigner, decodeToken, SECP256K1Client } from 'jsontokens'
    import 'cross-fetch/polyfill'
    import { fetchPrivate } from '../fetchUtil'

    /**
     * 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
     * @ignore
     */
    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
     * @ignore
     */
    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 fetchPrivate(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
     * @ignore
     */
    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')
      }

      try {
        const 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')
        }
        const payload = authRequestObject.payload
        if (typeof payload === 'string') {
          throw new Error('Unexpected token payload type of string')
        }

        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
        )
      } catch (e) {
        console.error(e.stack)
        return Promise.reject('Failed to parse authRequest in URL')
      }
    }