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

feature/auth : NbOAuth2AuthStrategy implementing basic authentication scheme against token endpoints #582

Merged
merged 8 commits into from
Jul 30, 2018
5 changes: 5 additions & 0 deletions src/framework/auth/strategies/oauth2/oauth2-strategy.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ describe('oauth2-auth-strategy', () => {

const successMessages = ['You have been successfully authenticated.'];
const errorMessages = ['Something went wrong, please try again.'];
const authHeader = 'Basic Y2xpZW50SWQ6Y2xpZW50U2VjcmV0';

const tokenSuccessResponse = {
access_token: '2YotnFZFEjr1zCsicMWpAA',
Expand Down Expand Up @@ -178,6 +179,7 @@ describe('oauth2-auth-strategy', () => {

httpMock.expectOne(
req => req.url === 'http://example.com/token'
&& req.headers.get('Authorization') === authHeader
&& req.body['grant_type'] === NbOAuth2GrantType.REFRESH_TOKEN
&& req.body['refresh_token'] === successToken.getRefreshToken()
&& !req.body['scope'],
Expand Down Expand Up @@ -394,6 +396,7 @@ describe('oauth2-auth-strategy', () => {

httpMock.expectOne(
req => req.url === 'http://example.com/custom'
&& req.headers.get('Authorization') === authHeader
&& req.body['grant_type'] === NbOAuth2GrantType.REFRESH_TOKEN
&& req.body['refresh_token'] === successToken.getRefreshToken()
&& req.body['scope'] === 'read',
Expand Down Expand Up @@ -456,6 +459,7 @@ describe('oauth2-auth-strategy', () => {

httpMock.expectOne(
req => req.url === 'http://example.com/token'
&& req.headers.get('Authorization') === authHeader
&& req.body['grant_type'] === NbOAuth2GrantType.PASSWORD
&& req.body['email'] === credentials.email
&& req.body['password'] === credentials.password,
Expand All @@ -480,6 +484,7 @@ describe('oauth2-auth-strategy', () => {

httpMock.expectOne(
req => req.url === 'http://example.com/token'
&& req.headers.get('Authorization') === authHeader
&& req.body['grant_type'] === NbOAuth2GrantType.PASSWORD
&& req.body['email'] === credentials.email
&& req.body['password'] === credentials.password,
Expand Down
22 changes: 18 additions & 4 deletions src/framework/auth/strategies/oauth2/oauth2-strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Licensed under the MIT License. See License.txt in the project root for license information.
*/
import { Inject, Injectable } from '@angular/core';
import { HttpClient, HttpErrorResponse } from '@angular/common/http';
import {HttpClient, HttpErrorResponse, HttpHeaders} from '@angular/common/http';
Copy link
Collaborator

Choose a reason for hiding this comment

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

spaces

import { ActivatedRoute } from '@angular/router';
import { Observable, of as observableOf } from 'rxjs';
import { switchMap, map, catchError } from 'rxjs/operators';
Expand Down Expand Up @@ -195,7 +195,7 @@ export class NbOAuth2AuthStrategy extends NbAuthStrategy {
refreshToken(token: NbAuthRefreshableToken): Observable<NbAuthResult> {
const url = this.getActionEndpoint('refresh');

return this.http.post(url, this.buildRefreshRequestData(token))
return this.http.post(url, this.buildRefreshRequestData(token), this.buildRequestsHttpOptions())
.pipe(
map((res) => {
return new NbAuthResult(
Expand All @@ -213,7 +213,7 @@ export class NbOAuth2AuthStrategy extends NbAuthStrategy {
passwordToken(email: string, password: string): Observable<NbAuthResult> {
const url = this.getActionEndpoint('token');

return this.http.post(url, this.buildPasswordRequestData(email, password))
return this.http.post(url, this.buildPasswordRequestData(email, password), this.buildRequestsHttpOptions() )
.pipe(
map((res) => {
return new NbAuthResult(
Expand All @@ -239,7 +239,7 @@ export class NbOAuth2AuthStrategy extends NbAuthStrategy {
protected requestToken(code: string) {
const url = this.getActionEndpoint('token');

return this.http.post(url, this.buildCodeRequestData(code))
return this.http.post(url, this.buildCodeRequestData(code), this.buildRequestsHttpOptions())
.pipe(
map((res) => {
return new NbAuthResult(
Expand Down Expand Up @@ -282,6 +282,20 @@ export class NbOAuth2AuthStrategy extends NbAuthStrategy {
return this.cleanParams(params);
}

protected buildRequestsHttpOptions(): any {
Copy link
Collaborator

Choose a reason for hiding this comment

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

let's make it optional?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This makes sense since clientId and clientSecret are optional

Copy link
Collaborator

Choose a reason for hiding this comment

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

Sorry, my comment was a bit misleading. What I meant is that probably we should add an option to the configuration as you proposed in the PR description indicating if we want to enable the base auth, pass clientid/cliensecret as body parameters or none of the above?

Copy link
Collaborator

Choose a reason for hiding this comment

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

@alain-charles just checking if you saw my comment :)

let httpOptions: any = {};
if (this.getOption('clientId') && this.getOption('clientSecret')) {
httpOptions = {
headers: new HttpHeaders (
{
'Authorization' : 'Basic ' + btoa(
this.getOption('clientId') + ':' + this.getOption('clientSecret')),
},
)};
}
return httpOptions;
}

protected handleResponseError(res: any): Observable<NbAuthResult> {
let errors = [];
if (res instanceof HttpErrorResponse) {
Expand Down
4 changes: 2 additions & 2 deletions src/playground/oauth2-password/oauth2-password.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ import { NbAuthOAuth2Token } from '@nebular/auth';
strategies: [
NbOAuth2AuthStrategy.setup({
name: 'password',
clientId: 'test',
clientSecret: 'secret',
clientId: 'Aladdin',
clientSecret: 'open sesame',
baseEndpoint: 'http://localhost:4400/api/auth/',
token: {
endpoint: 'token',
Expand Down