Commit ed53fa05 authored by Pau Ruiz i Safont's avatar Pau Ruiz i Safont
Browse files

feature: use sub instead of email as username

Also exposed email as it can be of use for the webapps
parent 6fdc794b
......@@ -95,6 +95,7 @@ import {
<div *ngIf="(credentials | async) as user; else loggedOut">
<p>Real name: {{ user.realname }}</p>
<p>Username: {{ user.username }}</p>
<p>Email: {{ user.email }}</p>
<p>Token: {{ user.token }}</p>
</div>
<ng-template #loggedOut>
......@@ -153,6 +154,7 @@ import {
export class AppComponent implements OnInit {
username: Observable < string | null > ;
realname: Observable < string | null > ;
email: Observable < string | null > ;
token: Observable < string | null > ;
isAuthenticated: Observable < string > ;
......@@ -162,6 +164,7 @@ export class AppComponent implements OnInit {
) {
this.username = auth.username();
this.realname = auth.realname();
this.email= auth.email();
this.token = auth.token();
this.isAuthenticated = auth.isAuthenticated().pipe(
......@@ -260,6 +263,7 @@ import {
<p>Real name: {{ realname|async }}</p>
<p>Username: {{ username|async }}</p>
<p>Email: {{ email|async }}</p>
<p>Expiration: {{ expiration|async }}</p>
<p>ISS: {{ iss|async }}</p>
<p>Token: {{ token|async }}</p>
......@@ -268,6 +272,7 @@ import {
export class AppComponent implements OnInit {
username: Observable < string | null > ;
realname: Observable < string | null > ;
email: Observable < string | null > ;
token: Observable < string | null > ;
// How to obtain other claims
......@@ -281,6 +286,7 @@ export class AppComponent implements OnInit {
) {
this.username = auth.username();
this.realname = auth.realname();
this.email = auth.email();
this.token = auth.token();
this.expiration = this.token.pipe(
......
......@@ -12,6 +12,7 @@
<div *ngIf="(credentials | async) as user; else loggedOut">
<p>Real name: {{ user.realname }}</p>
<p>Username: {{ user.username }}</p>
<p>Email: {{ user.email }}</p>
<p>Token: {{ user.token }}</p>
</div>
<ng-template #loggedOut>
......@@ -23,6 +24,7 @@
<p>Authenticated: {{ isAuthenticated|async }}</p>
<p>Real name: {{ realname|async }}</p>
<p>Username: {{ username|async }}</p>
<p>Email: {{ email|async }}</p>
<p>Expiration: {{ expiration|async }}</p>
<p>Token: {{ token|async }}</p>
</div>
......@@ -28,6 +28,7 @@ export class AppComponent implements OnInit {
// More specific
username: Observable < string | null > ;
realname: Observable < string | null > ;
email: Observable < string | null > ;
token: Observable < string | null > ;
isAuthenticated: Observable < string > ;
......@@ -43,6 +44,7 @@ export class AppComponent implements OnInit {
this.username = auth.username();
this.realname = auth.realname();
this.email = auth.email();
this.token = auth.token();
this.isAuthenticated = (auth.isAuthenticated()).pipe(
......
......@@ -65,7 +65,7 @@ describe('AuthService (valid token)', () => {
it('should have username', () => {
const username = service.username();
username.subscribe(result => expect(result).toEqual('test@ebi.ac.uk'));
username.subscribe(result => expect(result).toEqual('usr-75f4b000'));
});
it('should have realname', () => {
......@@ -73,6 +73,11 @@ describe('AuthService (valid token)', () => {
realname.subscribe(result => expect(result).toEqual('Ed Munden Gras'));
});
it('should have email', () => {
const email = service.email();
email.subscribe(result => expect(result).toEqual('test@ebi.ac.uk'));
});
it('should have token', () => {
const token = service.token();
token.subscribe(result => expect(result).toEqual(VALID_TOKEN));
......@@ -147,12 +152,17 @@ describe('AuthService (expired token)', () => {
username.subscribe(result => expect(result).toBeNull());
}));
it('should have realname', inject([AuthService], (service: AuthService) => {
it('should not have realname', inject([AuthService], (service: AuthService) => {
const realname = service.realname();
realname.subscribe(result => expect(result).toBeNull());
}));
it('should have token', inject([AuthService], (service: AuthService) => {
it('should not have email', inject([AuthService], (service: AuthService) => {
const email = service.email();
email.subscribe(result => expect(result).toBeNull());
}));
it('should not have token', inject([AuthService], (service: AuthService) => {
const token = service.token();
token.subscribe(result => expect(result).toBeNull());
}));
......
......@@ -29,6 +29,7 @@ export interface LoginOptions {
export interface Credentials {
realname: string;
username: string;
email: string;
token: string;
}
......@@ -75,33 +76,43 @@ export class AuthService {
}
public isAuthenticated(): Observable < boolean > {
return this._credentials.asObservable().pipe(
map(credentials => credentials ? true : false)
return this.fromCredentials(
credentials => credentials ? true : false
);
}
public credentials(): Observable < Credentials | null > {
return this._credentials.asObservable();
return this.fromCredentials(credentials => credentials);
}
public realname(): Observable < string | null > {
return this._credentials.asObservable().pipe(
map(credentials => credentials ? credentials.realname : null)
return this.fromCredentials(
credentials => credentials ? credentials.realname : null
);
}
public username(): Observable < string | null > {
return this._credentials.asObservable().pipe(
map(credentials => credentials ? credentials.username : null)
return this.fromCredentials(
credentials => credentials ? credentials.username : null
);
}
public email(): Observable < string | null > {
return this.fromCredentials(
credentials => credentials ? credentials.email : null
);
}
public token(): Observable < string | null > {
return this._credentials.asObservable().pipe(
map(credentials => credentials ? credentials.token : null)
return this.fromCredentials(
credentials => credentials ? credentials.token : null
);
}
private fromCredentials < T > (extractor: (_: Credentials) => T ): Observable < T > {
return this._credentials.asObservable().pipe(map(extractor));
}
/**
* Functions that opens a window instead of a tab.
*
......@@ -335,6 +346,7 @@ export class AuthService {
this._credentials.next({
realname: < string > this._getRealName(),
username: < string > this._getUserName(),
email: < string > this._getEmail(),
token: < string > this._getToken()
});
......@@ -365,12 +377,16 @@ export class AuthService {
return this._tokenService.getToken();
}
private _getRealName(): string | null {
return this._tokenService.getClaim < string, null > ('name', null);
}
private _getUserName(): string | null {
return this._tokenService.getClaim < string, null > ('email', null);
return this._tokenService.getClaim < string, null > ('sub', null);
}
private _getRealName(): string | null {
return this._tokenService.getClaim < string, null > ('name', null);
private _getEmail(): string | null {
return this._tokenService.getClaim < string, null > ('email', null);
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment