github.com/muhammedhassanm/blockchain@v0.0.0-20200120143007-697261defd4d/sawtooth-supply-chain-master/asset_client/src/services/api.js (about)

     1  /**
     2   * Copyright 2017 Intel Corporation
     3   *
     4   * Licensed under the Apache License, Version 2.0 (the "License");
     5   * you may not use this file except in compliance with the License.
     6   * You may obtain a copy of the License at
     7   *
     8   *     http://www.apache.org/licenses/LICENSE-2.0
     9   *
    10   * Unless required by applicable law or agreed to in writing, software
    11   * distributed under the License is distributed on an "AS IS" BASIS,
    12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13   * See the License for the specific language governing permissions and
    14   * limitations under the License.
    15   * ----------------------------------------------------------------------------
    16   */
    17  'use strict'
    18  
    19  const m = require('mithril')
    20  const _ = require('lodash')
    21  const sjcl = require('sjcl')
    22  
    23  const API_PATH = 'api/'
    24  const STORAGE_KEY = 'asset_track.authorization'
    25  let authToken = null
    26  
    27  /**
    28   * Generates a base-64 encoded SHA-256 hash of a plain text password
    29   * for submission to authorization routes
    30   */
    31  const hashPassword = password => {
    32    const bits = sjcl.hash.sha256.hash(password)
    33    return sjcl.codec.base64.fromBits(bits)
    34  }
    35  
    36  /**
    37   * Getters and setters to handle the auth token both in memory and storage
    38   */
    39  const getAuth = () => {
    40    if (!authToken) {
    41      authToken = window.localStorage.getItem(STORAGE_KEY)
    42    }
    43    return authToken
    44  }
    45  
    46  const setAuth = token => {
    47    window.localStorage.setItem(STORAGE_KEY, token)
    48    authToken = token
    49    return authToken
    50  }
    51  
    52  const clearAuth = () => {
    53    const token = getAuth()
    54    window.localStorage.clear(STORAGE_KEY)
    55    authToken = null
    56    return token
    57  }
    58  
    59  /**
    60   * Parses the authToken to return the logged in user's public key
    61   */
    62  const getPublicKey = () => {
    63    const token = getAuth()
    64    if (!token) return null
    65    return window.atob(token.split('.')[1])
    66  }
    67  
    68  // Adds Authorization header and prepends API path to url
    69  const baseRequest = opts => {
    70    const Authorization = getAuth()
    71    const authHeader = Authorization ? { Authorization } : {}
    72    opts.headers = _.assign(opts.headers, authHeader)
    73    opts.url = API_PATH + opts.url
    74    return m.request(opts)
    75  }
    76  
    77  /**
    78   * Submits a request to an api endpoint with an auth header if present
    79   */
    80  const request = (method, endpoint, data) => {
    81    return baseRequest({
    82      method,
    83      url: endpoint,
    84      data
    85    })
    86  }
    87  
    88  /**
    89   * Method specific versions of request
    90   */
    91  const get = _.partial(request, 'GET')
    92  const post = _.partial(request, 'POST')
    93  const patch = _.partial(request, 'PATCH')
    94  
    95  /**
    96   * Method for posting a binary file to the API
    97   */
    98  const postBinary = (endpoint, data) => {
    99    return baseRequest({
   100      method: 'POST',
   101      url: endpoint,
   102      headers: { 'Content-Type': 'application/octet-stream' },
   103      // prevent Mithril from trying to JSON stringify the body
   104      serialize: x => x,
   105      data
   106    })
   107  }
   108  
   109  module.exports = {
   110    hashPassword,
   111    getAuth,
   112    setAuth,
   113    clearAuth,
   114    getPublicKey,
   115    request,
   116    get,
   117    post,
   118    patch,
   119    postBinary
   120  }