Skip to content

Commit

Permalink
Refactored configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
zakhenry committed Jul 2, 2015
1 parent be68c6b commit 86b8b7d
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 68 deletions.
12 changes: 8 additions & 4 deletions README.md
Expand Up @@ -37,10 +37,14 @@ angular.module('app', ['ngJwtAuth'])
angular.module('app', ['ngJwtAuth'])
.config(['ngJwtAuthServiceProvider', function(ngJwtAuthServiceProvider){
ngJwtAuthServiceProvider
.setApiEndpoints({
base: '/api/auth/jwt',
login: '/login',
refresh: '/refresh'
.configure({
tokenLocation: 'token-custom',
apiEndpoints: {
base: '/api',
login: '/login-custom',
tokenExchange: '/token-custom',
refresh: '/refresh-custom',
}
})
;
}])
Expand Down
19 changes: 9 additions & 10 deletions dist/ngJwtAuth.d.ts
Expand Up @@ -16,7 +16,7 @@ declare module NgJwtAuth {
logout(): void;
}
interface INgJwtAuthServiceProvider {
setApiEndpoints(config: IEndpointDefinition): NgJwtAuthServiceProvider;
configure(config: INgJwtAuthServiceConfig): NgJwtAuthServiceProvider;
}
interface IEndpointDefinition {
base?: string;
Expand All @@ -25,13 +25,12 @@ declare module NgJwtAuth {
refresh?: string;
}
interface INgJwtAuthServiceConfig {
tokenLocation: string;
tokenUser: string;
loginController: string;
apiEndpoints: IEndpointDefinition;
storageKeyName: string;
refreshBeforeSeconds: number;
checkExpiryEverySeconds: number;
tokenLocation?: string;
tokenUser?: string;
apiEndpoints?: IEndpointDefinition;
storageKeyName?: string;
refreshBeforeSeconds?: number;
checkExpiryEverySeconds?: number;
}
interface IJwtToken {
header: {
Expand Down Expand Up @@ -271,11 +270,11 @@ declare module NgJwtAuth {
private config;
constructor();
/**
* Set the API endpoints for the auth service to call
* Set the configuration
* @param config
* @returns {NgJwtAuth.NgJwtAuthServiceProvider}
*/
setApiEndpoints(config: IEndpointDefinition): NgJwtAuthServiceProvider;
configure(config: IEndpointDefinition): NgJwtAuthServiceProvider;
$get: (string | (($http: any, $q: any, $window: any, $interval: any) => NgJwtAuthService))[];
}
}
7 changes: 3 additions & 4 deletions dist/ngJwtAuth.js

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

2 changes: 1 addition & 1 deletion dist/ngJwtAuth.js.map

Large diffs are not rendered by default.

15 changes: 7 additions & 8 deletions src/ngJwtAuthInterfaces.ts
Expand Up @@ -16,7 +16,7 @@ module NgJwtAuth {
}

export interface INgJwtAuthServiceProvider {
setApiEndpoints(config:IEndpointDefinition): NgJwtAuthServiceProvider;
configure(config:INgJwtAuthServiceConfig): NgJwtAuthServiceProvider;
}

export interface IEndpointDefinition {
Expand All @@ -27,13 +27,12 @@ module NgJwtAuth {
}

export interface INgJwtAuthServiceConfig {
tokenLocation: string;
tokenUser: string;
loginController: string;
apiEndpoints: IEndpointDefinition;
storageKeyName: string;
refreshBeforeSeconds: number;
checkExpiryEverySeconds: number;
tokenLocation?: string;
tokenUser?: string;
apiEndpoints?: IEndpointDefinition;
storageKeyName?: string;
refreshBeforeSeconds?: number;
checkExpiryEverySeconds?: number;
}

export interface IJwtToken {
Expand Down
10 changes: 6 additions & 4 deletions src/ngJwtAuthServiceProvider.ts
Expand Up @@ -32,13 +32,15 @@ module NgJwtAuth {

private config: INgJwtAuthServiceConfig;

/**
* Initialise the service provider
*/
constructor() {

//initialise service config
this.config = {
tokenLocation: 'token',
tokenUser: '#user',
loginController: 'app.public.login',
apiEndpoints: {
base: '/api/auth',
login: '/login',
Expand All @@ -53,12 +55,12 @@ module NgJwtAuth {
}

/**
* Set the API endpoints for the auth service to call
* Set the configuration
* @param config
* @returns {NgJwtAuth.NgJwtAuthServiceProvider}
*/
public setApiEndpoints(config:IEndpointDefinition) : NgJwtAuthServiceProvider {
this.config.apiEndpoints = _.defaults(config, this.config.apiEndpoints);
public configure(config:IEndpointDefinition) : NgJwtAuthServiceProvider {
this.config = _.defaults(config, this.config.apiEndpoints);
return this;
}

Expand Down
57 changes: 20 additions & 37 deletions test/test.ts
Expand Up @@ -52,31 +52,7 @@ let fixtures = {

get token(){

return fixtures.buildToken();

//let token:NgJwtAuth.IJwtToken;
//token = {
// header: {
// alg: 'RS256',
// typ: 'JWT'
// },
// data: {
// iss: 'api.spira.io',
// aud: 'spira.io',
// sub: fixtures.user.userId,
// iat: Number(moment().format('X')),
// exp: Number(moment().add(1, 'hours').format('X')),
// jti: 'random-hash',
// '#user': fixtures.userResponse,
// },
// signature: 'this-is-the-signed-hash'
//};
//
//return btoa(JSON.stringify(token.data))
// + '.' + btoa(JSON.stringify(token.data))
// + '.' + token.signature
//;

return fixtures.buildToken(); //no customisations
}
};

Expand Down Expand Up @@ -124,26 +100,34 @@ describe('Custom configuration', function () {

let authServiceProvider:NgJwtAuth.NgJwtAuthServiceProvider;
let customAuthService:NgJwtAuth.NgJwtAuthService;
let partialCustomConfig:NgJwtAuth.INgJwtAuthServiceConfig = {
tokenLocation: 'token-custom',
tokenUser: '#user-custom',
apiEndpoints: {
base: '/api/auth-custom',
login: '/login-custom',
tokenExchange: '/token-custom',
refresh: '/refresh-custom',
},
//storageKeyName: 'NgJwtAuthToken-custom', //intentionally commented out as this will be tested to be the default
};

beforeEach(() => {

module('ngJwtAuth', (_ngJwtAuthServiceProvider_) => {
authServiceProvider = _ngJwtAuthServiceProvider_; //register injection of service provider

authServiceProvider.setApiEndpoints({
base: 'mock/base/path/',
login: 'to/login',
refresh: 'to/refresh'
});

authServiceProvider.configure(partialCustomConfig);
});

});

it('should have the custom endpoints', () => {
expect((<any>authServiceProvider).config.apiEndpoints.base).to.equal('mock/base/path/');
expect((<any>authServiceProvider).config.apiEndpoints.login).to.equal('to/login');
expect((<any>authServiceProvider).config.apiEndpoints.refresh).to.equal('to/refresh');
it('should be able to partially configure the service provider', () => {

expect((<any>authServiceProvider).config.apiEndpoints).to.deep.equal(partialCustomConfig.apiEndpoints); //assert that the custom value has come across

expect((<any>authServiceProvider).config.storageKeyName).to.deep.equal((<any>authServiceProvider).config.storageKeyName); //assert that the default was not overridden

});

beforeEach(()=>{
Expand All @@ -153,7 +137,7 @@ describe('Custom configuration', function () {
});

it('should have the configured login endpoint', function() {
expect((<any>customAuthService).getLoginEndpoint()).to.equal('mock/base/path/to/login');
expect((<any>customAuthService).getLoginEndpoint()).to.equal('/api/auth-custom/login-custom');
});

});
Expand Down Expand Up @@ -551,7 +535,6 @@ describe('Service Reloading', () => {
;

if (latestRefresh <= nextRefreshOpportunity){ //after the interval that the token should have refreshed, flush the http request
console.log('flushing');
$httpBackend.flush();
}

Expand Down

0 comments on commit 86b8b7d

Please sign in to comment.