# All files / src/profiles/services service.js

87.5% Statements 28/32
70.59% Branches 12/17
81.82% Functions 9/11
87.1% Lines 27/31

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

    4x

    4x
    4x

    4x
    1x

    3x

    3x

    3x
    3x

    3x

    1x
    1x
    1x

    2x

    3x

    25x

    25x
    2x

    23x

    3x

    3x
    3x

    3x
    5x
    5x
    2x

    1x

     /* @flow */
    import 'cross-fetch/polyfill'
    import { containsValidProofStatement, containsValidAddressProofStatement } from './serviceUtils'

    export class Service {
      static validateProof(proof: Object,
                           ownerAddress: string,
                           name: ?string = null) {
        let proofUrl
        return Promise.resolve()
          .then(() => {
            proofUrl = this.getProofUrl(proof)
            return fetch(proofUrl)
          })
          .then((res) => {
            if (res.status !== 200) {
              throw new Error(`Proof url ${proofUrl} returned unexpected http status ${res.status}.
                  Unable to validate proof.`)
            }
            return res.text()
          })
          .then((text) => {
            // Validate identity in provided proof body/tags if required
            Iif (this.shouldValidateIdentityInBody()
                && proof.identifier !== this.getProofIdentity(text)) {
              return proof
            }
            const proofText = this.getProofStatement(text)
            proof.valid = containsValidProofStatement(proofText, name)
              || containsValidAddressProofStatement(proofText, ownerAddress)
            return proof
          })
          .catch((error) => {
            console.error(error)
            proof.valid = false
            return proof
          })
      }

      static getBaseUrls() {
        return []
      }

      static getProofIdentity(searchText: string) {
        return searchText
      }

      static getProofStatement(searchText: string) {
        return searchText
      }

      static shouldValidateIdentityInBody() {
        return false
      }

      static prefixScheme(proofUrl: string) {
        Iif (!proofUrl.startsWith('https://') && !proofUrl.startsWith('http://')) {
          return `https://${proofUrl}`
        } else if (proofUrl.startsWith('http://')) {
          return proofUrl.replace('http://', 'https://')
        } else {
          return proofUrl
        }
      }

      static getProofUrl(proof: Object) {
        const baseUrls = this.getBaseUrls()

        let proofUrl = proof.proof_url.toLowerCase()
        proofUrl = this.prefixScheme(proofUrl)

        for (let i = 0; i < baseUrls.length; i++) {
          const requiredPrefix = `${baseUrls[i]}${proof.identifier}`.toLowerCase()
          if (proofUrl.startsWith(requiredPrefix)) {
            return proofUrl
          }
        }
        throw new Error(`Proof url ${proof.proof_url} is not valid for service ${proof.service}`)
      }
    }