diff --git a/examples/passport-login/src/__tests__/acceptance/passport-login.acceptance.ts b/examples/passport-login/src/__tests__/acceptance/passport-login.acceptance.ts index 48bde337be76..1bb6511b876c 100644 --- a/examples/passport-login/src/__tests__/acceptance/passport-login.acceptance.ts +++ b/examples/passport-login/src/__tests__/acceptance/passport-login.acceptance.ts @@ -6,7 +6,7 @@ import {Client, supertest, expect} from '@loopback/testlab'; import {MockTestOauth2SocialApp} from '@loopback/authentication-passport'; import {ExpressServer} from '../../server'; -import {User} from '../../models'; +import {User, UserIdentity} from '../../models'; import {startApplication} from '../../'; import * as url from 'url'; import qs from 'qs'; @@ -65,7 +65,6 @@ describe('example-passport-login acceptance test', () => { describe('User login scenarios', () => { let Cookie: string; - let createdUser: User; /** *************************************** @@ -148,7 +147,20 @@ describe('example-passport-login acceptance test', () => { const users = response.body as User[]; expect(users.length).to.eql(1); expect(users[0].email).to.eql('test@example.com'); - createdUser = users[0]; + }); + + it('able to invoke api endpoints with basic auth', async () => { + await supertest('') + .get('http://localhost:3000/api/profiles') + .auth('test@example.com', 'password', {type: 'basic'}) + .expect(204); + }); + + it('basic auth fails for incorrect password', async () => { + await supertest('') + .get('http://localhost:3000/api/profiles') + .auth('test@example.com', 'incorrect-password', {type: 'basic'}) + .expect(401); }); }); @@ -256,13 +268,11 @@ describe('example-passport-login acceptance test', () => { }); it('check if profile is linked to existing user', async () => { - const filter = 'filter={"include":[{"relation": "profiles"}]}'; const response = await supertest('') - .get('http://localhost:3000/api/users/' + createdUser.id) - .query(filter); - const user = response.body as User; - expect(user.profiles?.length).to.eql(1); - const profiles = user.profiles ?? []; + .get('http://localhost:3000/api/profiles') + .auth('test@example.com', 'password', {type: 'basic'}); + const profiles = response.body as UserIdentity[]; + expect(profiles?.length).to.eql(1); expect(profiles[0].profile).to.eql({ emails: [{value: 'test@example.com'}], }); diff --git a/examples/passport-login/src/authentication-strategies/google.ts b/examples/passport-login/src/authentication-strategies/google.ts index 7f93d792bfc2..0d6d1774f993 100644 --- a/examples/passport-login/src/authentication-strategies/google.ts +++ b/examples/passport-login/src/authentication-strategies/google.ts @@ -49,32 +49,6 @@ export class GoogleOauth2Authorization implements AuthenticationStrategy { ); } - /** - * verify function for the oauth2 strategy - * - * @param accessToken - * @param refreshToken - * @param profile - * @param done - */ - verify( - accessToken: string, - refreshToken: string, - profile: Profile, - // eslint-disable-next-line @typescript-eslint/no-explicit-any - done: (error: any, user?: any, info?: any) => void, - ) { - // look up a linked user for the profile - this.userService - .findOrCreateUser(profile) - .then((user: User) => { - done(null, user); - }) - .catch((err: Error) => { - done(err); - }); - } - /** * authenticate a request * @param request diff --git a/examples/passport-login/src/authentication-strategies/oauth2.ts b/examples/passport-login/src/authentication-strategies/oauth2.ts index 28e35976f791..3568e71e45f0 100644 --- a/examples/passport-login/src/authentication-strategies/oauth2.ts +++ b/examples/passport-login/src/authentication-strategies/oauth2.ts @@ -62,32 +62,6 @@ export class Oauth2AuthStrategy implements AuthenticationStrategy { ); } - /** - * verify function for the oauth2 strategy - * - * @param accessToken - * @param refreshToken - * @param profile - * @param done - */ - verify( - accessToken: string, - refreshToken: string, - profile: Profile, - // eslint-disable-next-line @typescript-eslint/no-explicit-any - done: (error: any, user?: any, info?: any) => void, - ) { - // look up a linked user for the profile - this.userService - .findOrCreateUser(profile) - .then((user: User) => { - done(null, user); - }) - .catch((err: Error) => { - done(err); - }); - } - /** * authenticate a request * @param request diff --git a/examples/passport-login/src/controllers/user.controller.ts b/examples/passport-login/src/controllers/user.controller.ts index 6898e3412b24..51c60f48edf0 100644 --- a/examples/passport-login/src/controllers/user.controller.ts +++ b/examples/passport-login/src/controllers/user.controller.ts @@ -11,10 +11,11 @@ import { Response, RestBindings, RequestWithSession, + get, } from '@loopback/rest'; import {UserRepository} from '../repositories'; import {repository} from '@loopback/repository'; -import {SecurityBindings, UserProfile} from '@loopback/security'; +import {SecurityBindings, UserProfile, securityId} from '@loopback/security'; import {authenticate} from '@loopback/authentication'; import {UserCredentialsRepository} from '../repositories/user-credentials.repository'; import {UserIdentityRepository} from '../repositories/user-identity.repository'; @@ -130,4 +131,22 @@ export class UserLoginController { await this.userIdentityRepository.deleteAll(); await this.userRepository.deleteAll(); } + + @authenticate('basic') + @get('/profiles') + async getExternalProfiles( + @inject(SecurityBindings.USER) profile: UserProfile, + ) { + const user = await this.userRepository.findById( + parseInt(profile[securityId]), + { + include: [ + { + relation: 'profiles', + }, + ], + }, + ); + return user.profiles; + } }