Esempio n. 1
0
export function makeAuthRequest(privateKey,
                                domain_name,
                                manifestURI=null,
                                redirectURI=null,
                                scopes=[],
                                expiresAt=nextHour().getTime()) {
  let token = null

  if (domain_name === null) {
    throw new Error("Invalid app domain name")
  }
  if (manifestURI === null) {
    manifestURI = domain_name + '/manifest.json'
  }
  if (redirectURI === null) {
    redirectURI = domain_name
  }

  /* Create the payload */
  let payload = {
    jti: makeUUID4(),
    iat: Math.floor(new Date().getTime()/1000), // JWT times are in seconds
    exp: Math.floor(expiresAt/1000), // JWT times are in seconds
    iss: null,
    public_keys: [],
    domain_name: domain_name,
    manifest_uri: manifestURI,
    redirect_uri: redirectURI,
    scopes: scopes
  }

  if (privateKey === null) {
    /* Create an unsecured token and return it */
    token = createUnsecuredToken(payload)
  } else {
    /* Convert the private key to a public key to an issuer */
    const publicKey = SECP256K1Client.derivePublicKey(privateKey)
    payload.public_keys = [publicKey]
    const address = publicKeyToAddress(publicKey)
    payload.iss = makeDIDFromAddress(address)
    /* Sign and return the token */
    const tokenSigner = new TokenSigner('ES256k', privateKey)
    token = tokenSigner.sign(payload)
  }

  return token
}
Esempio n. 2
0
export function makeAuthResponse(privateKey,
                                 profile={},
                                 username=null,
                                 expiresAt=nextMonth().getTime()) {
  /* Convert the private key to a public key to an issuer */
  const publicKey = SECP256K1Client.derivePublicKey(privateKey)
  const address = publicKeyToAddress(publicKey)
  /* Create the payload */
  const payload = {
    jti: makeUUID4(),
    iat: Math.floor(new Date().getTime()/1000), // JWT times are in seconds
    exp: Math.floor(expiresAt/1000), // JWT times are in seconds
    iss: makeDIDFromAddress(address),
    public_keys: [publicKey],
    profile: profile,
    username: username
  }
  /* Sign and return the token */
  const tokenSigner = new TokenSigner('ES256k', privateKey)
  return tokenSigner.sign(payload)
}
.then((session) => {
  console.log('success!')
  console.log(session)

   // inspect session
  const token = jsontokens.decodeToken(session)
  const payload = token.payload

  console.log(JSON.stringify(payload));

  assert(payload.app_domain === 'www.foo.com')

  assert(payload.methods[0] === 'store_read')
  assert(payload.methods[1] === 'store_write')
  assert(payload.methods[2] === 'store_admin')
  assert(payload.methods.length === 3)

  assert(payload.app_public_keys.length == 1)
  assert(payload.app_public_keys[0]['public_key'] === jsontokens.SECP256K1Client.derivePublicKey(appPrivateKey))

  assert(payload.blockchain_id === 'judecn.id')
  return true
}, (error) => {