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

V2 endpoints #27

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
4 changes: 2 additions & 2 deletions src/aad.redirect.processor.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { QueryStringDeserializer } from './query.string.deserializer';
import { UserDecoder } from './user.decoder';
import { LocalStorage } from './local.storage';
import { SessionStorage } from './local.storage';
import { Constants } from './constants';
import { AadProductionTokenSample, AadProductionRedirectHash, AadProductionUserProfileSample } from './scenario/a.production.aad.response';
import { AadRedirectProcessor } from './aad.redirect.processor';
Expand All @@ -9,7 +9,7 @@ describe('AadRedirectProcessor', () => {
'use strict';

beforeEach(() => {
this.localStorage = new LocalStorage();
this.localStorage = new SessionStorage();
this.window = <Window>{ location: { hash: '' } };
this.window.location.assign = function () { };
this.userDecoder = new UserDecoder();
Expand Down
21 changes: 14 additions & 7 deletions src/aad.redirect.processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,24 @@ export class AadRedirectProcessor {
constructor(private queryStringDeserializer: QueryStringDeserializer, private userDecoder: UserDecoder, private storage: Storage, private window: Window) {
}

public process(): boolean {
public process(): string {

let deserializedHash = this.queryStringDeserializer.deserialize(this.window.location.hash);
let aadRedirect = new AadRedirectUrl(deserializedHash);
if (aadRedirect.isAadRedirect()) {
let userProfile = this.userDecoder.decode(aadRedirect.idToken || aadRedirect.accesToken);
this.storage.setItem(Constants.STORAGE.IDTOKEN, aadRedirect.idToken || '');
this.storage.setItem(Constants.STORAGE.ACCESSTOKEN, aadRedirect.accesToken || '');
this.window.location.assign(this.storage.getItem(Constants.STORAGE.LOGIN_REQUEST));
if (aadRedirect.state === this.storage.getItem(Constants.STORAGE.STATE_LOGIN)) {
let userProfile = this.userDecoder.decode(aadRedirect.idToken);
if (userProfile.nonce === this.storage.getItem(Constants.STORAGE.NONCE_IDTOKEN)) {
this.storage.setItem(Constants.STORAGE.IDTOKEN, aadRedirect.idToken || '');
this.storage.setItem(Constants.STORAGE.ACCESSTOKEN, aadRedirect.accessToken || '');
// TODO take expiresin and create timestamp for expiration.
this.storage.setItem(Constants.STORAGE.EXPIRATION_KEY, aadRedirect.expiresIn);
this.storage.setItem(Constants.STORAGE.SCOPE, aadRedirect.scope);
this.window.location.assign(this.storage.getItem(Constants.STORAGE.LOGIN_REQUEST));
return(this.storage.getItem(Constants.STORAGE.POST_LOGIN));
}
}
}

return aadRedirect.isAadRedirect();
return;
}
}
12 changes: 10 additions & 2 deletions src/aad.redirect.url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,23 @@ export class AadRedirectUrl {
return this.object[Constants.EXPIRES_IN];
}

get accesToken() {
get accessToken() {
return this.object[Constants.ACCESS_TOKEN];
}

get sessionState() {
return this.object[Constants.SESSION_STATE];
}

public isAadRedirect() {
get state() {
return this.object[Constants.STATE];
}

get scope() {
return this.object[Constants.SCOPE];
}

public isAadRedirect(): boolean {
return (
this.object.hasOwnProperty(Constants.ERROR_DESCRIPTION) ||
this.object.hasOwnProperty(Constants.ACCESS_TOKEN) ||
Expand Down
1 change: 1 addition & 0 deletions src/aad.url.builder.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ describe('AadUrlBuilder', () => {
responseType: 'id_token',
clientId: new GuidGenerator().generate(),
redirectUri: 'http://ng2a-hneu-web-ui.azurewebsites.net/',
scope: 'openid',
state: new GuidGenerator().generate(),
clientRequestId: new GuidGenerator().generate(),
libVersion: '1.0.0'
Expand Down
5 changes: 4 additions & 1 deletion src/aad.url.builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export class AadUrlBuilder {
private clientId: string;
private resource: string;
private redirectUri: string;
private scope: string;
private state: string;
private slice: string;
private clientRequestId: string;
Expand All @@ -33,6 +34,7 @@ export class AadUrlBuilder {
this.clientId = options.clientId;
this.responseType = options.responseType || this.responseType;
this.redirectUri = options.redirectUri || this.redirectUri;
this.scope = options.scope;
this.state = options.state;
this.slice = options.slice || this.slice;
this.clientRequestId = options.clientRequestId || this.clientRequestId;
Expand All @@ -43,7 +45,7 @@ export class AadUrlBuilder {

public build() {

var urlNavigate = AadUrlBuilder.MicrosoftLoginUrl + this.tenant + '/oauth2/authorize';
var urlNavigate = AadUrlBuilder.MicrosoftLoginUrl + this.tenant + '/oauth2/v2.0/authorize';
urlNavigate = urlNavigate + this.serialize() + this.addLibMetadata();
urlNavigate = urlNavigate + '&nonce=' + encodeURIComponent(this.nonce);
return urlNavigate;
Expand All @@ -59,6 +61,7 @@ export class AadUrlBuilder {
}

str.push('redirect_uri=' + encodeURIComponent(this.redirectUri));
str.push('scope=' + encodeURIComponent(this.scope));
str.push('state=' + encodeURIComponent(this.state));

if (this.slice) {
Expand Down
1 change: 1 addition & 0 deletions src/aad.url.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ export interface AadUrlConfig {
clientId: string;
responseType?: string;
redirectUri?: string;
scope: string;
state: string;
slice?: string;
clientRequestId?: string;
Expand Down
18 changes: 12 additions & 6 deletions src/adal.config.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
export class AdalConfig {
public resource: string;
constructor(public clientId: string,
public tenant: string,
public redirectUri: string,
public postLogoutRedirectUrl?: string,
public responseType?: string,
public extraQueryParameter?: string) {
constructor(public tenant: string,
public clientId: string,
public responseType: string,
public redirectUri: string,
public scope: string,
public response_mode?: string,
public state?: string,
public nonce?: string,
public prompt?: string,
public login_hint?: string,
public domain_hint?: string,
public postLogoutRedirectUrl?: string) {
};
}
4 changes: 2 additions & 2 deletions src/authentication.context.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/// <reference path="./../node_modules/@types/jasmine/index.d.ts" />
import { AuthenticationContext } from './authentication.context';
import { LocalStorage } from './local.storage';
import { SessionStorage } from './local.storage';
import { Navigator } from './navigator';
import { AadUrlBuilder } from './aad.url.builder';
import { AadLogoutUrlBuilder } from './aad.logout.url.builder';
Expand All @@ -16,7 +16,7 @@ describe('AuthenticationContext', () => {

beforeEach(() => {
this.config = ATenantConfig;
this.localStorage = new LocalStorage();
this.localStorage = new SessionStorage();
this.navigator = new Navigator();
this.guidGenerator = new GuidGenerator();
this.aadUrlBuilder = new AadUrlBuilder(this.guidGenerator);
Expand Down
8 changes: 5 additions & 3 deletions src/authentication.context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,12 @@ export class AuthenticationContext {
this.loginInProgress = true;
}

public getUser(): User {
let idtoken = this.storage.getItem(Constants.STORAGE.IDTOKEN) || this.storage.getItem(Constants.STORAGE.ACCESSTOKEN);
public getUser(token?: string): User {
if (token == null) {
token = this.storage.getItem(Constants.STORAGE.IDTOKEN) || this.storage.getItem(Constants.STORAGE.ACCESSTOKEN);
}
try {
let user = this.userDecoder.decode(idtoken);
let user = this.userDecoder.decode(token);
return user;
} catch (error) {
if (console && console.debug) console.debug('getUser() returns null on catched error. Details >> ' + error.toString());
Expand Down
2 changes: 1 addition & 1 deletion src/authentication.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/// <reference path="./../node_modules/@types/jasmine/index.d.ts" />
import { AuthenticationContext } from './authentication.context';
import { Authentication } from './authentication';
import { LocalStorage } from './local.storage';
import { SessionStorage } from './local.storage';
import { AdalConfig } from './adal.config';
import { Navigator } from './navigator';
import { AadUrlBuilder } from './aad.url.builder';
Expand Down
8 changes: 3 additions & 5 deletions src/authentication.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { AuthenticationContext } from './authentication.context';
import { LocalStorage } from './local.storage';
import { SessionStorage } from './local.storage';
import { Navigator } from './navigator';
import { AadUrlBuilder } from './aad.url.builder';
import { GuidGenerator } from './guid.generator';
Expand All @@ -18,11 +18,9 @@ export class Authentication {

public static getContext(configuration: AdalConfig): AuthenticationContext {

console.log('getContext...');

let context = new AuthenticationContext(
configuration,
new LocalStorage(),
new SessionStorage(),
new Navigator(),
new GuidGenerator(),
new AadUrlBuilder(new GuidGenerator()),
Expand All @@ -37,7 +35,7 @@ export class Authentication {
let p = new AadRedirectProcessor(
new QueryStringDeserializer(),
new UserDecoder(),
new LocalStorage(),
new SessionStorage(),
window);
return p;
}
Expand Down
6 changes: 5 additions & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ export const Constants = {
ID_TOKEN: 'id_token',
ERROR_DESCRIPTION: 'error_description',
SESSION_STATE: 'session_state',
STATE: 'state',
SCOPE: 'scope',
STORAGE: {
TOKEN_KEYS: 'adal.token.keys',
ACCESS_TOKEN_KEY: 'adal.access.token.key',
Expand All @@ -19,7 +21,9 @@ export const Constants = {
ERROR_DESCRIPTION: 'adal.error.description',
LOGIN_REQUEST: 'adal.login.request',
LOGIN_ERROR: 'adal.login.error',
RENEW_STATUS: 'adal.token.renew.status'
RENEW_STATUS: 'adal.token.renew.status',
SCOPE: 'adal.scope',
POST_LOGIN: 'adal.post.login'
},
RESOURCE_DELIMETER: '|',
LOADFRAME_TIMEOUT: '6000',
Expand Down
6 changes: 3 additions & 3 deletions src/local.storage.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Storage } from './storage';
export class LocalStorage implements Storage {
export class SessionStorage implements Storage {
public setItem(key: string, value: string): void {
localStorage.setItem(key, value);
sessionStorage.setItem(key, value);
}

public getItem(key: string): string {
return localStorage.getItem(key);
return sessionStorage.getItem(key);
}
}
4 changes: 2 additions & 2 deletions src/scenario/a.production.adal.config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { AdalConfig } from '../adal.config';

export const ATenantConfig = new AdalConfig('61bdbb45-4004-48e3-4444-e4f1740661c8', 'unittest.onmicrosoft.com', 'http://localhost/login', 'http://localhost/logout');
export const ATenantConfig_AcessToken = new AdalConfig('0113255e-cecc-4ad1-b101-08822ea5d0da', 'friden.onmicrosoft.com', 'http://localhost:4200', '', 'token', 'resource=https://friden.sharepoint.com');
export const ATenantConfig = new AdalConfig('unittest.onmicrosoft.com', '61bdbb45-4004-48e3-4444-e4f1740661c8', 'http://localhost/login', 'id_token', 'openid', 'fragment', '', '', 'http://localhost/logout');
export const ATenantConfig_AcessToken = new AdalConfig('friden.onmicrosoft.com', '0113255e-cecc-4ad1-b101-08822ea5d0da', 'http://localhost:4200', 'token', 'openid', 'fragment', '', 'resource=https://friden.sharepoint.com');

export const ATenantUrl = 'https://login.microsoftonline.com/unittest.onmicrosoft.com/oauth2/authorize?response_type=id_token&' +
'client_id=61bdbb45-4004-48e3-4444-e4f1740661c8&redirect_uri=http%3A%2F%2Flocalhost&state=xxx&client-request-id=xxx&x-client-SKU=Js&x-client-Ver=1.0.0&nonce=xxx';
Expand Down
1 change: 1 addition & 0 deletions src/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export interface User {
nonce?: string;
oid?: string;
platf?: string;
profile?: any;
sub?: string;
tid?: string;
unique_name: string;
Expand Down