Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(provider): inject USER_MODEL key to user-authentication provider #109

Merged
merged 1 commit into from
Nov 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,12 @@ export class User extends Entity implements IAuthUser {
}
```

Now bind this model to USER_MODEL key in application.ts

```ts
this.bind(AuthenticationBindings.USER_MODEL).to(User);
```

Create CRUD repository for the above model. Use loopback CLI.

```sh
Expand Down Expand Up @@ -460,6 +466,12 @@ export class User extends Entity implements IAuthUser {
}
```

Now bind this model to USER_MODEL key in application.ts

```ts
this.bind(AuthenticationBindings.USER_MODEL).to(User);
```

Create CRUD repository for the above model. Use loopback CLI.

```sh
Expand Down Expand Up @@ -668,6 +680,12 @@ export class User extends Entity implements IAuthUser {
}
```

Now bind this model to USER_MODEL key in application.ts

```ts
this.bind(AuthenticationBindings.USER_MODEL).to(User);
```

Create CRUD repository for both of the above models. Use loopback CLI.

```sh
Expand Down Expand Up @@ -1064,6 +1082,12 @@ export class User extends Entity implements IAuthUser {
}
```

Now bind this model to USER_MODEL key in application.ts

```ts
this.bind(AuthenticationBindings.USER_MODEL).to(User);
```

Create CRUD repository for the above model. Use loopback CLI.

```sh
Expand Down Expand Up @@ -1371,6 +1395,12 @@ export class User extends Entity implements IAuthUser {
}
```

Now bind this model to USER_MODEL key in application.ts

```ts
this.bind(AuthenticationBindings.USER_MODEL).to(User);
```

Create CRUD repository for the above model. Use loopback CLI.

```sh
Expand Down Expand Up @@ -1676,6 +1706,12 @@ export class User extends Entity implements IAuthUser {
}
```

Now bind this model to USER_MODEL key in application.ts

```ts
this.bind(AuthenticationBindings.USER_MODEL).to(User);
```

Create CRUD repository for the above model. Use loopback CLI.

```sh
Expand Down Expand Up @@ -1983,6 +2019,12 @@ export class User extends Entity implements IAuthUser {
}
```

Now bind this model to USER_MODEL key in application.ts

```ts
this.bind(AuthenticationBindings.USER_MODEL).to(User);
```

Create CRUD repository for the above model. Use loopback CLI.

```sh
Expand Down Expand Up @@ -2288,6 +2330,12 @@ export class User extends Entity implements IAuthUser {
}
```

Now bind this model to USER_MODEL key in application.ts

```ts
this.bind(AuthenticationBindings.USER_MODEL).to(User);
```

Create CRUD repository for the above model. Use loopback CLI.

```sh
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {Entity, model, property} from '@loopback/repository';

@model({settings: {strict: false}})
export class Authuser extends Entity {
export class UserCred extends Entity {
@property({
type: 'string',
id: true,
Expand All @@ -16,19 +16,13 @@ export class Authuser extends Entity {
})
password: string;

// Define well-known properties here

// Indexer property to allow additional data
// eslint-disable-next-line @typescript-eslint/no-explicit-any
[prop: string]: any;

constructor(data?: Partial<Authuser>) {
constructor(data?: Partial<UserCred>) {
super(data);
}
}

export interface AuthuserRelations {
export interface UserCredRelations {
// describe navigational properties here
}

export type AuthuserWithRelations = Authuser & AuthuserRelations;
export type AuthuserWithRelations = UserCred & UserCredRelations;
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {Strategies} from '../../../../strategies/keys';
import {LocalVerifyProvider} from '../../../fixtures/providers/local-password.provider';
import {AuthenticationBindings} from '../../../../keys';
import {IAuthUser} from '../../../../types';
import {Authuser} from '../../../../models';
import {UserCred} from '../../../fixtures/user-cred.model';
/**
* Testing overall flow of authentication with bearer strategy
*/
Expand All @@ -25,7 +25,10 @@ describe('Local passport strategy', () => {
class TestController {
@post('/auth/local/no-user-data-passed')
@authenticate(STRATEGY.LOCAL)
test(@requestBody({required: true}) body: Authuser) {
test(
@requestBody({required: true})
body: UserCred,
) {
return 'test successful';
}
}
Expand All @@ -43,7 +46,7 @@ describe('Local passport strategy', () => {
@authenticate(STRATEGY.LOCAL)
test(
@requestBody()
body: Authuser,
body: UserCred,
) {
return 'test successful';
}
Expand All @@ -66,7 +69,7 @@ describe('Local passport strategy', () => {

@post('/auth/local/no-options')
@authenticate(STRATEGY.LOCAL)
test(@requestBody() body: {username: string; password: string}) {
test(@requestBody() body: UserCred) {
return this.user;
}
}
Expand Down Expand Up @@ -94,7 +97,7 @@ describe('Local passport strategy', () => {

@post('/auth/local/pass-req-callback-true')
@authenticate(STRATEGY.LOCAL, {passReqToCallback: true})
async test(@requestBody() body: {username: string; password: string}) {
async test(@requestBody() body: UserCred) {
return this.user;
}
}
Expand Down Expand Up @@ -122,7 +125,7 @@ describe('Local passport strategy', () => {

@post('/auth/local/pass-req-callback-false')
@authenticate(STRATEGY.LOCAL, {passReqToCallback: false})
async test(@requestBody() body: {username: string; password: string}) {
async test(@requestBody() body: UserCred) {
return this.user;
}
}
Expand All @@ -144,7 +147,7 @@ describe('Local passport strategy', () => {

@post('/auth/local/null-user')
@authenticate(STRATEGY.LOCAL)
async test(@requestBody() body: {username: string; password: string}) {
async test(@requestBody() body: UserCred) {
return body;
}
}
Expand Down Expand Up @@ -192,7 +195,7 @@ describe('Local strategy with no verifier', () => {

@post('/auth/local/no-verifier')
@authenticate(STRATEGY.LOCAL, {passReqToCallback: false})
async test(@requestBody() body: {username: string; password: string}) {
async test(@requestBody() body: UserCred) {
return body;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {Strategies} from '../../../../strategies/keys';
import {LocalVerifyProvider} from '../../../fixtures/providers/local-password.provider';
import {AuthenticationBindings} from '../../../../keys';
import {IAuthUser} from '../../../../types';
import {Authuser} from '../../../../models';
import {UserCred} from '../../../fixtures/user-cred.model';
/**
* Testing overall flow of authentication with bearer strategy
*/
Expand All @@ -25,7 +25,10 @@ describe('Local passport strategy using Middleware Sequence', () => {
class TestController {
@post('/auth/local/no-user-data-passed')
@authenticate(STRATEGY.LOCAL)
test(@requestBody({required: true}) body: Authuser) {
test(
@requestBody({required: true})
body: UserCred,
) {
return 'test successful';
}
}
Expand All @@ -43,7 +46,7 @@ describe('Local passport strategy using Middleware Sequence', () => {
@authenticate(STRATEGY.LOCAL)
test(
@requestBody()
body: Authuser,
body: UserCred,
) {
return 'test successful';
}
Expand All @@ -66,7 +69,7 @@ describe('Local passport strategy using Middleware Sequence', () => {

@post('/auth/local/no-options')
@authenticate(STRATEGY.LOCAL)
test(@requestBody() body: {username: string; password: string}) {
test(@requestBody() body: UserCred) {
return this.user;
}
}
Expand Down Expand Up @@ -94,7 +97,7 @@ describe('Local passport strategy using Middleware Sequence', () => {

@post('/auth/local/pass-req-callback-true')
@authenticate(STRATEGY.LOCAL, {passReqToCallback: true})
async test(@requestBody() body: {username: string; password: string}) {
async test(@requestBody() body: UserCred) {
return this.user;
}
}
Expand Down Expand Up @@ -122,7 +125,7 @@ describe('Local passport strategy using Middleware Sequence', () => {

@post('/auth/local/pass-req-callback-false')
@authenticate(STRATEGY.LOCAL, {passReqToCallback: false})
async test(@requestBody() body: {username: string; password: string}) {
async test(@requestBody() body: UserCred) {
return this.user;
}
}
Expand All @@ -144,7 +147,7 @@ describe('Local passport strategy using Middleware Sequence', () => {

@post('/auth/local/null-user')
@authenticate(STRATEGY.LOCAL)
async test(@requestBody() body: {username: string; password: string}) {
async test(@requestBody() body: UserCred) {
return body;
}
}
Expand Down Expand Up @@ -192,7 +195,7 @@ describe('Local strategy with no verifier using Middleware Sequence', () => {

@post('/auth/local/no-verifier')
@authenticate(STRATEGY.LOCAL, {passReqToCallback: false})
async test(@requestBody() body: {username: string; password: string}) {
async test(@requestBody() body: UserCred) {
return body;
}
}
Expand Down
1 change: 0 additions & 1 deletion src/models/index.ts

This file was deleted.

20 changes: 13 additions & 7 deletions src/providers/user-authentication.provider.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import {Getter, inject, Provider, Setter} from '@loopback/context';
import {Constructor, Getter, inject, Provider, Setter} from '@loopback/context';
import {HttpErrors, Request, Response} from '@loopback/rest';
import {Strategy} from 'passport';

import {AuthErrorKeys} from '../error-keys';
import {AuthenticationBindings} from '../keys';
import {StrategyAdapter} from '../strategy-adapter';
import {AuthenticateFn, IAuthUser, AuthenticationMetadata} from '../types';
import {
AuthenticateFn,
IAuthUser,
AuthenticationMetadata,
EntityWithIdentifier,
} from '../types';

export class AuthenticateActionProvider
implements Provider<AuthenticateFn<IAuthUser | undefined>>
Expand All @@ -17,6 +22,8 @@ export class AuthenticateActionProvider
private readonly getMetadata: Getter<AuthenticationMetadata>,
@inject.setter(AuthenticationBindings.CURRENT_USER)
readonly setCurrentUser: Setter<IAuthUser | undefined>,
@inject(AuthenticationBindings.USER_MODEL, {optional: true})
public authUserModel?: Constructor<EntityWithIdentifier & IAuthUser>,
) {}

value(): AuthenticateFn<IAuthUser | undefined> {
Expand Down Expand Up @@ -45,12 +52,11 @@ export class AuthenticateActionProvider
authOpts = metadata.authOptions(request);
}
const strategyAdapter = new StrategyAdapter<IAuthUser>(strategy);
const user = await strategyAdapter.authenticate(
request,
response,
authOpts,
);
let user = await strategyAdapter.authenticate(request, response, authOpts);
if (user) {
if (this.authUserModel) {
user = new this.authUserModel(user);
}
this.setCurrentUser(user);
return user;
}
Expand Down