All files / src/wallet-sdk/auth verification.ts

38.59% Statements 44/114
21.05% Branches 12/57
31.25% Functions 5/16
41.5% Lines 44/106

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 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328                                1x 1x 1x                       1x                                 1x                                                                           4x 4x 4x 4x     1x 1x 1x 1x 1x 1x 1x 1x                                       1x 1x 1x 1x   1x   1x 1x 1x 1x                                             1x                                                                                         1x 1x 1x 1x 1x 1x                             1x 1x 1x 1x 1x 1x                         1x                       1x                                       1x                                               1x                                   1x                                      
/**
 * Checks if the ES256k signature on passed `token` match the claimed public key
 * in the payload key `public_keys`.
 *
 * @param  {String} token encoded and signed authentication token
 * @return {Boolean} Returns `true` if the signature matches the claimed public key
 * @throws {Error} if `token` contains multiple public keys
 * @private
 * @ignore
 */
import {
  c32ToB58,
  decodeToken,
  Json,
  publicKeyToStxAddress,
  TokenVerifier,
} from 'micro-stacks/crypto';
import { getAddressFromDID } from 'micro-stacks/connect';
import { fetchPrivate } from 'micro-stacks/common';
 
/**
 * 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
 * @ignore
 */
export async function fetchAppManifest(authRequest: string): Promise<any> {
  Iif (!authRequest) {
    throw new Error('Invalid auth request');
  }
  const payload = getValidTokenPayload(authRequest);
  const manifestURI = payload.manifest_uri as string;
  try {
    const response = await fetchPrivate(manifestURI);
    const responseText = await response.text();
    const responseJSON = JSON.parse(responseText);
    return { ...responseJSON, manifestURI };
  } catch (error) {
    console.log(error);
    throw new Error('Could not fetch manifest.json');
  }
}
 
export function isSameOriginAbsoluteUrl(uri1: string, uri2: string) {
  try {
    const parsedUri1 = new URL(uri1);
    const parsedUri2 = new URL(uri2);
 
    const port1 =
      parseInt(parsedUri1.port || '0', 10) | 0 || (parsedUri1.protocol === 'https:' ? 443 : 80);
    const port2 =
      parseInt(parsedUri2.port || '0', 10) | 0 || (parsedUri2.protocol === 'https:' ? 443 : 80);
 
    const match = {
      scheme: parsedUri1.protocol === parsedUri2.protocol,
      hostname: parsedUri1.hostname === parsedUri2.hostname,
      port: port1 === port2,
      absolute:
        (uri1.includes('http://') || uri1.includes('https://')) &&
        (uri2.includes('http://') || uri2.includes('https://')),
    };
 
    return match.scheme && match.hostname && match.port && match.absolute;
  } catch (error) {
    console.log(error);
    console.log('Parsing error in same URL origin check');
    // Parse error
    return false;
  }
}
 
interface TokenPayload {
  [key: string]: Json;
 
  iss: string;
  jti: string;
  iat: string | number;
  exp: string | number;
}
 
function getValidTokenPayload(token: string): TokenPayload {
  const payload = decodeToken(token)?.payload;
  Iif (!payload) throw new Error('Unexpected undefined payload of token');
  Iif (typeof payload === 'string') throw new Error('Unexpected token payload type of string');
  return payload as TokenPayload;
}
 
export function doSignaturesMatchPublicKeys(token: string): boolean {
  const payload = getValidTokenPayload(token);
  const publicKeys = payload.public_keys as string[];
  Iif (!publicKeys || publicKeys.length === 0) throw new Error('Unexpected public keys value');
  if (publicKeys.length === 1) {
    const publicKey = publicKeys[0];
    try {
      return new TokenVerifier('ES256k', publicKey).verify(token);
    } catch (e) {
      return false;
    }
  } else E{
    throw new Error('Multiple public keys are not supported');
  }
}
 
/**
 * Makes sure that the identity address portion of
 * the decentralized identifier passed in the issuer `iss`
 * key of the token matches the public key
 *
 * @param  {String} token encoded and signed authentication token
 * @return {Boolean} if the identity address and public keys match
 * @throws {Error} if ` token` has multiple public keys
 * @private
 * @ignore
 */
export function doPublicKeysMatchIssuer(token: string): boolean {
  const payload = getValidTokenPayload(token);
  const publicKeys = payload.public_keys as string[];
  Iif (!publicKeys || publicKeys.length === 0) throw new Error('Unexpected public keys value');
 
  const addressFromIssuer = getAddressFromDID(payload.iss);
 
  if (publicKeys.length === 1) {
    const addressFromPublicKeys = publicKeyToStxAddress(publicKeys[0]);
    if (addressFromPublicKeys === addressFromIssuer) {
      return true;
    }
  } else E{
    throw new Error('Multiple public keys are not supported');
  }
 
  return false;
}
 
/**
 * Looks up the identity address that owns the claimed username
 * in `token` using the lookup endpoint provided in `nameLookupURL`
 * to determine if the username is owned by the identity address
 * that matches the claimed public key
 *
 * @param  {String} token  encoded and signed authentication token
 * @param  {String} nameLookupURL a URL to the name lookup endpoint of the Blockstack Core API
 * @return {Promise<Boolean>} returns a `Promise` that resolves to
 * `true` if the username is owned by the public key, otherwise the
 * `Promise` resolves to `false`
 * @private
 * @ignore
 */
export async function doPublicKeysMatchUsername(
  token: string,
  nameLookupURL: string
): Promise<boolean> {
  try {
    const payload = getValidTokenPayload(token);
 
    Iif (!payload.username) return true;
    Iif (nameLookupURL === null) return false;
 
    const username = payload.username;
    const url = `${nameLookupURL.replace(/\/$/, '')}/${username}`;
    const response = await fetchPrivate(url);
    const responseText = await response.text();
    const responseJSON = JSON.parse(responseText);
    if (responseJSON.hasOwnProperty('address')) {
      const nameOwningAddress = responseJSON.address;
      let nameOwningAddressBtc = nameOwningAddress;
      try {
        // try converting STX to BTC
        // if this throws, it's already a BTC address
        nameOwningAddressBtc = c32ToB58(nameOwningAddress as string);
      } catch {}
      const addressFromIssuer = getAddressFromDID(payload.iss);
      return nameOwningAddressBtc === addressFromIssuer;
    } else {
      return false;
    }
  } catch (error) {
    console.log(error);
    console.log('Error checking `doPublicKeysMatchUsername`');
    return false;
  }
}
 
/**
 * Checks if the if the token issuance time and date is after the
 * current time and date.
 *
 * @param  {String}  token encoded and signed authentication token
 * @return {Boolean} `true` if the token was issued after the current time,
 * otherwise returns `false`
 * @private
 * @ignore
 */
export function isIssuanceDateValid(token: string) {
  const payload = getValidTokenPayload(token);
  if (payload.iat) {
    Iif (typeof payload.iat !== 'number') return false;
    const issuedAt = new Date(payload.iat * 1000); // JWT times are in seconds
    return new Date().getTime() >= issuedAt.getTime();
  } else E{
    return true;
  }
}
 
/**
 * Checks if the expiration date of the `token` is before the current time
 * @param  {String}  token encoded and signed authentication token
 * @return {Boolean} `true` if the `token` has not yet expired, `false`
 * if the `token` has expired
 *
 * @private
 * @ignore
 */
export function isExpirationDateValid(token: string) {
  const payload = getValidTokenPayload(token);
  if (payload.exp) {
    Iif (typeof payload.exp !== 'number') return false;
    const expiresAt = new Date(payload.exp * 1000); // JWT times are in seconds
    return new Date().getTime() <= expiresAt.getTime();
  } else E{
    return true;
  }
}
 
/**
 * Makes sure the `manifest_uri` is a same origin absolute URL.
 * @param  {String}  token encoded and signed authentication token
 * @return {Boolean} `true` if valid, otherwise `false`
 * @private
 * @ignore
 */
export function isManifestUriValid(token: string) {
  const payload = getValidTokenPayload(token);
  return isSameOriginAbsoluteUrl(payload.domain_name as string, payload.manifest_uri as string);
}
 
/**
 * Makes sure the `redirect_uri` is a same origin absolute URL.
 * @param  {String}  token encoded and signed authentication token
 * @return {Boolean} `true` if valid, otherwise `false`
 * @private
 * @ignore
 */
export function isRedirectUriValid(token: string) {
  const payload = getValidTokenPayload(token);
  return isSameOriginAbsoluteUrl(payload.domain_name as string, payload.redirect_uri as string);
}
 
/**
 * Verify authentication request is valid. This function performs a number
 * of checks on the authentication request token:
 * * Checks that `token` has a valid issuance date & is not expired
 * * Checks that `token` has a valid signature that matches the public key it claims
 * * Checks that both the manifest and redirect URLs are absolute and conform to
 * the same origin policy
 *
 * @param  {String} token encoded and signed authentication request token
 * @return {Promise} that resolves to true if the auth request
 *  is valid and false if it does not. It rejects with a String if the
 *  token is not signed
 * @private
 * @ignore
 */
export async function verifyAuthRequest(token: string): Promise<boolean> {
  Iif (decodeToken(token)?.header.alg === 'none') {
    throw new Error('Token must be signed in order to be verified');
  }
  const values = await Promise.all([
    isExpirationDateValid(token),
    isIssuanceDateValid(token),
    doSignaturesMatchPublicKeys(token),
    doPublicKeysMatchIssuer(token),
    isManifestUriValid(token),
    isRedirectUriValid(token),
  ]);
  return values.every(val => val);
}
 
/**
 * Verify the authentication request is valid and
 * fetch the app manifest file if valid. Otherwise, reject the promise.
 * @param  {String} token encoded and signed authentication request token
 * @return {Promise} that resolves to the app manifest file in JSON format
 * or rejects if the auth request or app manifest file is invalid
 * @private
 * @ignore
 */
export async function verifyAuthRequestAndLoadManifest(token: string): Promise<any> {
  const valid = await verifyAuthRequest(token);
  Iif (!valid) {
    throw new Error('Token is an invalid auth request');
  }
  return fetchAppManifest(token);
}
 
/**
 * Verify the authentication response is valid.
 * @param {String} token the authentication response token
 * @param {String} nameLookupURL the url use to verify owner of a username
 * @param fallbackLookupURLs an optional array of name lookup URLs to check usernames for
 * @return {Promise} that resolves to true if auth response
 * is valid and false if it does not
 * @private
 * @ignore
 */
export async function verifyAuthResponse(
  token: string,
  nameLookupURL: string,
  fallbackLookupURLs?: string[]
): Promise<boolean> {
  const values = await Promise.all([
    isExpirationDateValid(token),
    isIssuanceDateValid(token),
    doSignaturesMatchPublicKeys(token),
    doPublicKeysMatchIssuer(token),
  ]);
  const usernameMatchings = await Promise.all(
    [nameLookupURL]
      .concat(fallbackLookupURLs || [])
      .map(url => doPublicKeysMatchUsername(token, url))
  );
  const someUsernameMatches = usernameMatchings.includes(true);
  return someUsernameMatches && values.every(val => val);
}