diff --git a/extensions/authentication-jwt/package.json b/extensions/authentication-jwt/package.json index afa8fe2bd5ec..ee4e1a784ea0 100644 --- a/extensions/authentication-jwt/package.json +++ b/extensions/authentication-jwt/package.json @@ -28,8 +28,7 @@ "@loopback/service-proxy": "^2.3.7", "@types/bcryptjs": "2.4.2", "bcryptjs": "^2.4.3", - "jsonwebtoken": "^8.5.1", - "@loopback/context": "^3.8.1" + "jsonwebtoken": "^8.5.1" }, "devDependencies": { "@loopback/boot": "^2.5.0", diff --git a/extensions/authentication-jwt/src/__tests__/acceptance/jwt.component.test.ts b/extensions/authentication-jwt/src/__tests__/acceptance/jwt.component.test.ts index c81a64f72e43..cae9b90fed2a 100644 --- a/extensions/authentication-jwt/src/__tests__/acceptance/jwt.component.test.ts +++ b/extensions/authentication-jwt/src/__tests__/acceptance/jwt.component.test.ts @@ -54,7 +54,7 @@ describe('jwt authentication', () => { it(`user login and token granted successfully`, async () => { const credentials = {email: 'jane@doe.com', password: 'opensesame'}; const res = await client - .post('/users/refresh/login') + .post('/users/refresh-token') .send(credentials) .expect(200); refreshToken = res.body.refreshToken; diff --git a/extensions/authentication-jwt/src/__tests__/fixtures/controllers/user.controller.ts b/extensions/authentication-jwt/src/__tests__/fixtures/controllers/user.controller.ts index 872d55b933a4..52ff0620fd12 100644 --- a/extensions/authentication-jwt/src/__tests__/fixtures/controllers/user.controller.ts +++ b/extensions/authentication-jwt/src/__tests__/fixtures/controllers/user.controller.ts @@ -9,22 +9,22 @@ import { UserService, } from '@loopback/authentication'; import {inject} from '@loopback/core'; +import {model, property} from '@loopback/repository'; import {get, post, requestBody} from '@loopback/rest'; import {SecurityBindings, securityId, UserProfile} from '@loopback/security'; +import {genSalt, hash} from 'bcryptjs'; import { + RefreshGrant, + RefreshGrantRequestBody, + RefreshTokenServiceBindings, + TokenObject, TokenServiceBindings, User, UserServiceBindings, - TokenObject, - RefreshGrantRequestBody, - RefreshGrant, - RefreshTokenServiceBindings, } from '../../../'; -import {model, property} from '@loopback/repository'; -import {genSalt, hash} from 'bcryptjs'; import {UserRepository} from '../../../repositories'; import {Credentials} from '../../../services/user.service'; -import {RefreshTokenService} from '../../../keys'; +import {RefreshTokenService} from '../../../types'; const CredentialsSchema = { type: 'object', @@ -154,7 +154,7 @@ export class UserController { return this.user[securityId]; } // Routes using refreshtoken - @post('/users/refresh/login', { + @post('/users/refresh-token', { responses: { '200': { description: 'Token', diff --git a/extensions/authentication-jwt/src/index.ts b/extensions/authentication-jwt/src/index.ts index 9a66b82b84eb..141367bdf58a 100644 --- a/extensions/authentication-jwt/src/index.ts +++ b/extensions/authentication-jwt/src/index.ts @@ -8,3 +8,4 @@ export * from './keys'; export * from './models'; export * from './repositories'; export * from './services'; +export * from './types'; diff --git a/extensions/authentication-jwt/src/keys.ts b/extensions/authentication-jwt/src/keys.ts index 62ee60366a28..0df87b0bab02 100644 --- a/extensions/authentication-jwt/src/keys.ts +++ b/extensions/authentication-jwt/src/keys.ts @@ -7,7 +7,7 @@ import {TokenService, UserService} from '@loopback/authentication'; import {BindingKey} from '@loopback/core'; import {User} from './models'; import {Credentials} from './services/user.service'; -import {UserProfile} from '@loopback/security'; +import {RefreshTokenService} from './types'; export namespace TokenServiceConstants { export const TOKEN_SECRET_VALUE = 'myjwts3cr3t'; @@ -57,42 +57,3 @@ export namespace RefreshTokenServiceBindings { export const DATASOURCE_NAME = 'refreshdb'; export const REFRESH_REPOSITORY = 'repositories.RefreshTokenRepository'; } - -export type RefreshGrant = { - refreshToken: string; -}; - -export const RefreshGrantSchema = { - type: 'object', - required: ['refreshToken'], - properties: { - refreshToken: { - type: 'string', - }, - }, -}; -export const RefreshGrantRequestBody = { - description: 'Reissuing Acess Token', - required: true, - content: { - 'application/json': {schema: RefreshGrantSchema}, - }, -}; - -export type TokenObject = { - accessToken: string; - expiresIn?: string | undefined; - refreshToken?: string | undefined; -}; - -export interface RefreshTokenService { - /** - * Generate Token and return the Token Object - */ - generateToken(userProfile: UserProfile, token: string): Promise; - /** - * Verifies the validity of a token string and returns a new Token - * - */ - refreshToken(refreshToken: string): Promise; -} diff --git a/extensions/authentication-jwt/src/services/refreshtoken.service.ts b/extensions/authentication-jwt/src/services/refreshtoken.service.ts index f4b63a9e2cc6..f0bdcfcd0ab3 100644 --- a/extensions/authentication-jwt/src/services/refreshtoken.service.ts +++ b/extensions/authentication-jwt/src/services/refreshtoken.service.ts @@ -1,18 +1,19 @@ -import {bind, inject, BindingScope, uuid} from '@loopback/core'; +import {TokenService} from '@loopback/authentication'; +import {bind, BindingScope, inject, uuid} from '@loopback/core'; +import {repository} from '@loopback/repository'; +import {HttpErrors} from '@loopback/rest'; +import {securityId, UserProfile} from '@loopback/security'; +import {promisify} from 'util'; import { RefreshTokenServiceBindings, - TokenObject, - UserServiceBindings, TokenServiceBindings, + UserServiceBindings, } from '../keys'; -import {repository} from '@loopback/repository'; -import {UserProfile, securityId} from '@loopback/security'; -import {promisify} from 'util'; -import {HttpErrors} from '@loopback/rest'; -import {TokenService} from '@loopback/authentication'; import {RefreshTokenRepository} from '../repositories'; +import {TokenObject} from '../types'; import {MyUserService} from './user.service'; /* eslint-disable*/ + import {RefreshToken, RefreshTokenRelations} from '../models'; /* eslint-enable */ const jwt = require('jsonwebtoken'); diff --git a/extensions/authentication-jwt/src/types.ts b/extensions/authentication-jwt/src/types.ts new file mode 100644 index 000000000000..30555f69f1ca --- /dev/null +++ b/extensions/authentication-jwt/src/types.ts @@ -0,0 +1,45 @@ +// Copyright IBM Corp. 2020. All Rights Reserved. +// Node module: @loopback/authentication-jwt +// This file is licensed under the MIT License. +// License text available at https://opensource.org/licenses/MIT + +import {UserProfile} from '@loopback/security'; + +export type RefreshGrant = { + refreshToken: string; +}; + +export const RefreshGrantSchema = { + type: 'object', + required: ['refreshToken'], + properties: { + refreshToken: { + type: 'string', + }, + }, +}; +export const RefreshGrantRequestBody = { + description: 'Reissuing Acess Token', + required: true, + content: { + 'application/json': {schema: RefreshGrantSchema}, + }, +}; + +export type TokenObject = { + accessToken: string; + expiresIn?: string | undefined; + refreshToken?: string | undefined; +}; + +export interface RefreshTokenService { + /** + * Generate Token and return the Token Object + */ + generateToken(userProfile: UserProfile, token: string): Promise; + /** + * Verifies the validity of a token string and returns a new Token + * + */ + refreshToken(refreshToken: string): Promise; +} diff --git a/extensions/authentication-jwt/tsconfig.json b/extensions/authentication-jwt/tsconfig.json index 3a055c271482..8ada1a68b1b0 100644 --- a/extensions/authentication-jwt/tsconfig.json +++ b/extensions/authentication-jwt/tsconfig.json @@ -17,9 +17,6 @@ { "path": "../../packages/boot/tsconfig.json" }, - { - "path": "../../packages/context/tsconfig.json" - }, { "path": "../../packages/core/tsconfig.json" },