Skip to content
This repository has been archived by the owner on Dec 8, 2022. It is now read-only.

Commit

Permalink
Added ability to specify a permission scope to SkyAuthHttp (#245)
Browse files Browse the repository at this point in the history
  • Loading branch information
Blackbaud-PaulCrowder authored Jul 26, 2017
1 parent b647924 commit eb1f438
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"@angular/platform-browser": "4.2.5",
"@angular/platform-browser-dynamic": "4.2.5",
"@angular/router": "4.2.5",
"@blackbaud/auth-client": "1.9.1",
"@blackbaud/auth-client": "1.10.1",
"@blackbaud/help-client": "1.0.1",
"@ngtools/webpack": "1.3.1",
"@types/core-js": "0.9.41",
Expand Down
15 changes: 15 additions & 0 deletions runtime/auth-http.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,19 @@ describe('SkyAuthHttp', () => {
expect(skyAuthHttp).toBeDefined();
});

it('should request a token with the specified permission scope', () => {
const search = '?envid=1234';
const getTokenSpy = spyOn(BBAuth, 'getToken');

setupInjector(search);
skyAuthHttp
.withScope('abc')
.get('example.com');

expect(getTokenSpy).toHaveBeenCalledWith({
envId: '1234',
permissionScope: 'abc'
});
});

});
57 changes: 54 additions & 3 deletions runtime/auth-http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,73 @@ import { Observable } from 'rxjs/Observable';
import 'rxjs/add/observable/fromPromise';
import 'rxjs/add/operator/mergeMap';

import { BBAuthGetTokenArgs } from '@blackbaud/auth-client';

import { SkyAppConfig } from '@blackbaud/skyux-builder/runtime/config';
import { SkyAuthTokenProvider } from '@blackbaud/skyux-builder/runtime/auth-token-provider';

/**
* Makes authenticated web requests to Blackbaud web services using a BBID token.
*/
@Injectable()
export class SkyAuthHttp extends Http {

private permissionScope: string;

constructor(
backend: ConnectionBackend,
defaultOptions: RequestOptions,
private backend: ConnectionBackend,
private defaultOptions: RequestOptions,
private authTokenProvider: SkyAuthTokenProvider,
private skyAppConfig: SkyAppConfig
) {
super(backend, defaultOptions);
}

/**
* Adds the specified permission scope to the token that can then be passed to the
* backend web service.
* @param permissionScope The permission scope to include.
*/
public withScope(permissionScope: string): SkyAuthHttp {
// There was no clean way to allow permission scope to be specified with all the various
// convenience methods such as get(), post(), etc. that the base Http class provides,
// so this chainable method just creates a new instance of SkyAuthHttp with a permissionScope
// property set. When chained, the end call would look something like this:
// http.withScope('abc').get(url);

const http = new SkyAuthHttp(
this.backend,
this.defaultOptions,
this.authTokenProvider,
this.skyAppConfig
);

http.permissionScope = permissionScope;

return http;
}

/**
* Makes an authenticated request to a Blackbaud service.
* @param url The URL to request.
* @param options The request options.
*/
public request(
url: string | Request,
options?: RequestOptionsArgs
): Observable<Response> {
return Observable.fromPromise(this.authTokenProvider.getToken())
let tokenArgs: BBAuthGetTokenArgs;

// See if this call was chained to withScope(), and if so, provide it when
// retrieving a token.
if (this.permissionScope) {
tokenArgs = {
envId: this.getEnvId(),
permissionScope: this.permissionScope
};
}

return Observable.fromPromise(this.authTokenProvider.getToken(tokenArgs))
.flatMap((token: string) => {
let authOptions: Request | RequestOptionsArgs;

Expand All @@ -61,4 +108,8 @@ export class SkyAuthHttp extends Http {
return super.request(url, authOptions);
});
}

private getEnvId() {
return this.skyAppConfig.runtime.params.get('envid');
}
}
6 changes: 3 additions & 3 deletions runtime/auth-token-provider.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { BBAuth } from '@blackbaud/auth-client';
import { BBAuth, BBAuthGetTokenArgs } from '@blackbaud/auth-client';

export class SkyAuthTokenProvider {

public getToken(): Promise<string> {
return BBAuth.getToken();
public getToken(args?: BBAuthGetTokenArgs): Promise<string> {
return BBAuth.getToken(args);
}

}

0 comments on commit eb1f438

Please sign in to comment.