Skip to content
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
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 32 additions & 0 deletions src/__tests__/fixtures/providers/cognito-auth.provider.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import {Provider} from '@loopback/context';
import {Request} from '@loopback/rest';
import {Cognito, IAuthUser, VerifyFunction} from '../../../types';

/**
* A provider for default implementation of VerifyFunction.CognitoAuthFn
*
* It will just throw an error saying Not Implemented
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't look like it will do this 🙂

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is mock-verify-provider naa. It will just return the user (like other verify-providers).
This comment is from the actual verify provider, forgot to remove it. 😅

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok 🙁

*/
export class BearerTokenVerifyProvider
implements Provider<VerifyFunction.CognitoAuthFn>
{
constructor() {}

value(): VerifyFunction.CognitoAuthFn {
return async (
accessToken: string,
refreshToken: string,
profile: Cognito.Profile,
cb: Cognito.VerifyCallback,
req?: Request,
) => {
const userToPass: IAuthUser = {
id: 1,
username: 'xyz',
password: 'pass',
};

return userToPass;
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import {Client, createClientForHandler} from '@loopback/testlab';
import {RestServer, Request} from '@loopback/rest';
import {Application, Provider} from '@loopback/core';
import {get} from '@loopback/openapi-v3';
import {authenticate} from '../../../../decorators';
import {STRATEGY} from '../../../../strategy-name.enum';
import {getApp} from '../helpers/helpers';
import {MyAuthenticationSequence} from '../../../fixtures/sequences/authentication.sequence';
import {Strategies} from '../../../../strategies/keys';
import {VerifyFunction} from '../../../../strategies';
import {userWithoutReqObj} from '../../../fixtures/data/bearer-data';
import {Cognito} from '../../../../types';

describe('getting cognito oauth2 strategy with options', () => {
let app: Application;
let server: RestServer;
beforeEach(givenAServer);
beforeEach(givenAuthenticatedSequence);
beforeEach(getAuthVerifier);

it('should return 302 when client id is passed and passReqToCallback is set true', async () => {
class TestController {
@get('/test')
@authenticate(STRATEGY.COGNITO_OAUTH2, {
clientID: 'string',
clientSecret: 'string',
passReqToCallback: true,
})
test() {
return 'test successful';
}
}

app.controller(TestController);

await whenIMakeRequestTo(server).get('/test').expect(302);
});

function whenIMakeRequestTo(restServer: RestServer): Client {
return createClientForHandler(restServer.requestHandler);
}

async function givenAServer() {
app = getApp();
server = await app.getServer(RestServer);
}

function getAuthVerifier() {
app
.bind(Strategies.Passport.COGNITO_OAUTH2_VERIFIER)
.toProvider(CognitoAuthVerifyProvider);
}

function givenAuthenticatedSequence() {
// bind user defined sequence
server.sequence(MyAuthenticationSequence);
}
});

class CognitoAuthVerifyProvider
implements Provider<VerifyFunction.CognitoAuthFn>
{
constructor() {}

value(): VerifyFunction.CognitoAuthFn {
return async (
accessToken: string,
refreshToken: string,
profile: Cognito.Profile,
cd: Cognito.VerifyCallback,
req?: Request,
) => {
return userWithoutReqObj;
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import {Client, createClientForHandler} from '@loopback/testlab';
import {RestServer, Request} from '@loopback/rest';
import {Application, Provider} from '@loopback/core';
import {get} from '@loopback/openapi-v3';
import {authenticate} from '../../../../decorators';
import {STRATEGY} from '../../../../strategy-name.enum';
import {getApp} from '../helpers/helpers';
import {Strategies} from '../../../../strategies/keys';
import {VerifyFunction} from '../../../../strategies';
import {userWithoutReqObj} from '../../../fixtures/data/bearer-data';
import {Cognito} from '../../../../types';
import {MyAuthenticationMiddlewareSequence} from '../../../fixtures/sequences/authentication-middleware.sequence';

describe('getting cognito oauth2 strategy with options using Middleware Sequence', () => {
let app: Application;
let server: RestServer;
beforeEach(givenAServer);
beforeEach(givenAuthenticatedSequence);
beforeEach(getAuthVerifier);

it('should return 302 when client id is passed and passReqToCallback is set true', async () => {
class TestController {
@get('/test')
@authenticate(STRATEGY.COGNITO_OAUTH2, {
clientID: 'string',
clientSecret: 'string',
passReqToCallback: true,
})
test() {
return 'test successful';
}
}

app.controller(TestController);

await whenIMakeRequestTo(server).get('/test').expect(302);
});

function whenIMakeRequestTo(restServer: RestServer): Client {
return createClientForHandler(restServer.requestHandler);
}

async function givenAServer() {
app = getApp();
server = await app.getServer(RestServer);
}

function getAuthVerifier() {
app
.bind(Strategies.Passport.COGNITO_OAUTH2_VERIFIER)
.toProvider(CognitoAuthVerifyProvider);
}

function givenAuthenticatedSequence() {
// bind user defined sequence
server.sequence(MyAuthenticationMiddlewareSequence);
}
});

class CognitoAuthVerifyProvider
implements Provider<VerifyFunction.CognitoAuthFn>
{
constructor() {}

value(): VerifyFunction.CognitoAuthFn {
return async (
accessToken: string,
refreshToken: string,
profile: Cognito.Profile,
cd: Cognito.VerifyCallback,
req?: Request,
) => {
return userWithoutReqObj;
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import {Cognito, IAuthUser} from '../../../types';
import {expect} from '@loopback/testlab';
import {
CognitoStrategyFactoryProvider,
CognitoAuthStrategyFactory,
} from '../../../strategies/passport/passport-cognito-oauth2';

describe('getting cognito-auth strategy with options', () => {
it('should return strategy by passing options and passReqToCallback as true', async () => {
const strategyVerifier: CognitoAuthStrategyFactory = await getStrategy();

const options: Cognito.StrategyOptions = {
callbackURL: 'string',
clientDomain: 'string',
clientID: 'string',
clientSecret: 'string',
region: 'string',
passReqToCallback: true,
};

const cognitoAuthStrategyVerifier = strategyVerifier(options);

expect(cognitoAuthStrategyVerifier).to.have.property('name');
expect(cognitoAuthStrategyVerifier)
.to.have.property('authenticate')
.which.is.a.Function();
});

it('should return strategy by passing options and passReqToCallback as false', async () => {
const strategyVerifier: CognitoAuthStrategyFactory = await getStrategy();

const options: Cognito.StrategyOptions = {
callbackURL: 'string',
clientDomain: 'string',
clientID: 'string',
clientSecret: 'string',
region: 'string',
passReqToCallback: false,
};

const cognitoAuthStrategyVerifier = strategyVerifier(options);

expect(cognitoAuthStrategyVerifier).to.have.property('name');
expect(cognitoAuthStrategyVerifier)
.to.have.property('authenticate')
.which.is.a.Function();
});
});

async function getStrategy() {
const provider = new CognitoStrategyFactoryProvider(verifierBearer);

//this fuction will return a function which will then accept options.
return provider.value();
}

//returning a user
function verifierBearer(
accessToken: string,
refreshToken: string,
profile: Cognito.Profile,
): Promise<IAuthUser | null> {
const userToPass: IAuthUser = {
id: 1,
username: 'xyz',
password: 'pass',
};

return new Promise(function (resolve, reject) {
if (userToPass) {
resolve(userToPass);
}
});
}