jsonwebtoken#Secret TypeScript Examples

The following examples show how to use jsonwebtoken#Secret. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: user_failure.ts    From api-example with MIT License 6 votes vote down vote up
describe('login', () => {
  it('should return internal_server_error if jwt.sign fails with the error', async () => {
    const sign = jwt.sign;
    (jwt.sign as any) = (payload: string | Buffer | object,
      secretOrPrivateKey: Secret,
      options: SignOptions,
      callback: SignCallback) => {
        callback(new Error('failure'), undefined)
    }

    const dummy = await createDummy()
    await expect(user.login(dummy.email, dummy.password)).rejects.toEqual({
      error: {type: 'internal_server_error', message: 'Internal Server Error'}
    })
    jwt.sign = sign
  })

  it('should return JWT token, userId, expireAt to a valid login/password if cacheExternal rejects', async () => {
    (cacheExternal.setProp as jest.Mock).mockRejectedValueOnce(new Error('connection error'));
    const dummy = await createDummy()
    await expect(user.login(dummy.email, dummy.password)).resolves.toEqual({
      userId: dummy.userId,
      token: expect.stringMatching(/^[A-Za-z0-9-_=]+\.[A-Za-z0-9-_=]+\.?[A-Za-z0-9-_.+/=]*$/),
      expireAt: expect.any(Date)
    })
  })
})
Example #2
Source File: getSignedJwt.ts    From tezos-academy with MIT License 6 votes vote down vote up
getSignedJwt: GetSignedJwt = (_id, username, userRole) => {
  const expiresAt: Date = dayjs().add(30, 'day').toDate()

  const payload: JwtPayload = {
    _id,
    username,
    userRole,
    expiresAt,
  }

  const signOptions: SignOptions = {
    issuer: 'TezosAcademy',
    subject: username + '',
    audience: 'https://tezosacademy.io',
    expiresIn: '30d',
    algorithm: 'RS256',
  }

  const jwt: Jwt = jsonwebtoken.sign(payload, process.env.JWT_PRIVATE_KEY as Secret, signOptions)

  return jwt
}
Example #3
Source File: verifySignedJwt.ts    From tezos-academy with MIT License 6 votes vote down vote up
verifySignedJwt: VerifySignedJwt = (jwt) => {
  const jwtDecoded: JwtDecoded = jsonwebtoken.decode(jwt) as JwtDecoded

  const verifyOptions = {
    issuer: 'TezosAcademy',
    subject: jwtDecoded ? jwtDecoded.username : undefined,
    audience: 'https://tezosacademy.io',
    expiresIn: '30d',
    algorithm: ['RS256'],
  }

  const jwtPayload: JwtPayload = jsonwebtoken.verify(
    jwt,
    process.env.JWT_PUBLIC_KEY as Secret,
    verifyOptions,
  ) as JwtPayload

  return jwtPayload
}
Example #4
Source File: jwt.ts    From ts-oauth2-server with MIT License 5 votes vote down vote up
constructor(private readonly secretOrPrivateKey: Secret) {}