Skip to content

Commit 51ab66f

Browse files
Adding and implementing services, models and flows.
1 parent baa3256 commit 51ab66f

19 files changed

+314
-30
lines changed

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,8 @@
2828
"gulp": "^3.9.1",
2929
"gulp-typescript": "^3.1.5",
3030
"typescript": "^2.2.1"
31+
},
32+
"dependencies": {
33+
"randomstring": "^1.1.5"
3134
}
3235
}

source/FlowFactory.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,29 @@ class FlowFactory {
1313
private _flowTypeMapList:FlowTypeMap[];
1414

1515
/**
16-
*
16+
* Get a new flow instance builded with the given type and data.
17+
* @param type Represents the type of flow to instantiate.
18+
* @param data Represents the data to deserialize into the flow.
1719
*/
1820
public getFlow(type:string, data:any):IFlow {
1921

2022
let map:FlowTypeMap = this._flowTypeMapList.find(function(map) { return map.type == type });
2123

2224
if (!map)
23-
throw new Error(`The type [{type}] dont have any asociated build function in the factory.`);
25+
throw new Error('The type [' + type + '] dont have any asociated build function in the factory.');
2426

2527
return map.buildFunction(data);
2628
}
2729

2830
/**
29-
*
31+
* Set the given build function as a builder for the given type.
32+
* @param type Represents the type to assing the builder function.
33+
* @param buildFunction Represents the function the build the flow of the given type.
3034
*/
31-
public addCustomFlow(type:string, buildFunction:(data:any) => IFlow):void {
35+
public setFlowTypeBuilder(type:string, buildFunction:(data:any) => IFlow):void {
3236

3337
if (this._flowTypeMapList.find(function(map) { return map.type == type }))
34-
throw new Error(`The type [{type}] already has a asociated build function in the factory.`);
38+
throw new Error('The type [' + type + '] already has a asociated build function in the factory.');
3539

3640

3741
this._flowTypeMapList.push({
@@ -41,6 +45,4 @@ class FlowFactory {
4145
}
4246
}
4347

44-
let factoryInstance = new FlowFactory();
45-
46-
export = factoryInstance;
48+
export = new FlowFactory();

source/Flows/IFlow.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ export interface IFlow {
77
/**
88
* Execute the flow and handle it with the given handler.
99
*/
10-
execute(handler:IFlowHandler):void
10+
execute(handler:IFlowHandler):Promise<void>
1111
}

source/Flows/PasswordFlow.ts

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,30 @@
11
import { BaseFlow } from './BaseFlow'
2+
import { FlowResult } from '../Models/FlowResult'
23
import { IFlowHandler } from '../IFlowHandler'
4+
import { Token } from "../Models/Persistents/Token";
5+
import { ITokenService } from "../Services/ITokenService";
6+
import { IUserService } from "../Services/IUserService";
7+
38

49
/**
510
* Represents the password flow.
611
*/
712
export class PasswordFlow extends BaseFlow {
8-
constructor() {
9-
super('password')
13+
14+
private _userService:IUserService;
15+
private _tokenService:ITokenService;
16+
17+
/**
18+
* Create a new instance of password flow class.
19+
* @param userService Represents a user service.
20+
*/
21+
constructor(userService:IUserService
22+
,tokenService:ITokenService) {
23+
24+
super('password');
25+
26+
this._userService = userService;
27+
this._tokenService = tokenService;
1028
}
1129

1230
/**
@@ -22,8 +40,22 @@ export class PasswordFlow extends BaseFlow {
2240
* Execute the flow and handle it with the given handler.
2341
* @param handler Represents the handler for the flow.
2442
*/
25-
public execute(handler:IFlowHandler):void {
43+
public async execute(handler:IFlowHandler):Promise<void> {
44+
try
45+
{
46+
if (await this._userService.validateCredential(this.username, this.password)) {
47+
let token:Token = await this._tokenService.generateToken();
48+
let result:FlowResult = new FlowResult();
49+
50+
result.accessToken = token.value;
51+
result.expireIn = token.expireIn;
52+
result.refreshToken = token.refreshToken ? token.refreshToken.value : null;
2653

27-
54+
handler.returnResult(result);
55+
}
56+
}
57+
catch(ex) {
58+
handler.notifyError('');
59+
}
2860
}
2961
}

source/IFlowHandler.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ export interface IFlowHandler {
77
/**
88
* Redirect the request to another page, usually the authorization page.
99
*/
10-
redirect(url:string):void
10+
redirectToURL(url:string):void
1111
/**
1212
* Return the flow result to the requester client.
1313
*/
14-
return(result:FlowResult):void
14+
returnResult(result:FlowResult):void
1515
/**
1616
* Notify an error that may occur.
1717
*/
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* Represents a base token.
3+
*/
4+
export abstract class BaseToken {
5+
6+
/**
7+
* Get or set the value of the token.
8+
*/
9+
public value:string;
10+
11+
/**
12+
* The time in seconds to the token expiration.
13+
*/
14+
public expireIn:number;
15+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { BaseToken } from './BaseToken'
2+
3+
/**
4+
* Represents a refresh token.
5+
*/
6+
export class RefreshToken extends BaseToken {
7+
8+
}

source/Models/Persistents/Token.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1-
export class Token {
1+
import { BaseToken } from './BaseToken'
2+
3+
/**
4+
* Represents a token.
5+
*/
6+
export class Token extends BaseToken {
27

8+
/**
9+
* Get or set the refresh token information.
10+
*/
11+
public refreshToken:BaseToken;
312
}

source/Models/Persistents/User.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,15 @@
1+
/**
2+
* Represents an user.
3+
*/
14
export class User {
25

6+
/**
7+
* Get or set the user name.
8+
*/
9+
public name:string;
10+
11+
/**
12+
* Get or set the user password.
13+
*/
14+
public password:string;
315
}

source/OAuthContext.ts

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { OAuthContextConfiguration } from './OAuthContextConfiguration'
2+
3+
/**
4+
* Represents an OAuth context.
5+
*/
6+
export class OAuthContext {
7+
8+
private _configuration:OAuthContextConfiguration;
9+
10+
/**
11+
* Create a new instance of the OAuth context with a default configuration.
12+
*/
13+
constructor() {
14+
this.loadConfiguration();
15+
}
16+
17+
/**
18+
* Get the context configuration.
19+
*/
20+
public get configuration() { return this._configuration; }
21+
22+
private loadConfiguration() {
23+
this._configuration = new OAuthContextConfiguration();
24+
this._configuration.tokenExpirationTime = null;
25+
this._configuration.refreshTokenExpirationTime = null;
26+
this._configuration.authorizationCodeExpirationTime = 30;
27+
this._configuration.mustGenerateRefreshToken = false;
28+
}
29+
}

source/OAuthContextConfiguration.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/**
2+
* Represents a set of configurations for an OAuth context;
3+
*/
4+
export class OAuthContextConfiguration {
5+
6+
/**
7+
* Get or set the amount of time in second for a token to expire;
8+
*/
9+
public tokenExpirationTime:number;
10+
11+
/**
12+
* Get or set the amount of time in second for a refresh token to expire;
13+
*/
14+
public refreshTokenExpirationTime:number;
15+
16+
/**
17+
* Get or set the amount of time in second for an authorization code to expire;
18+
*/
19+
public authorizationCodeExpirationTime:number;
20+
21+
/**
22+
* Get or set a boolean value specifying if the token generation process must create an refresh token;
23+
*/
24+
public mustGenerateRefreshToken:boolean;
25+
}
Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
import { Token } from '../Models/Persistents/Token'
22

3-
export interface IUserRepository {
3+
/**
4+
* Represents a token repository to persists tokens.
5+
*/
6+
export interface ITokenRepository {
47

5-
getAll():Promise<Array<Token>>;
8+
/**
9+
* Get a tokent instance by the given value.
10+
* @param value Represents the value to look for.
11+
*/
12+
getByValue(value:string):Promise<Token>;
613

7-
getList(name:string, limit:number, offset:number):Promise<Array<Token>>;
8-
9-
getById(id:string):Promise<Token>;
10-
11-
save(client:Token):Promise<void>;
14+
/**
15+
* Save or update the token instance;
16+
* @param token The token instance to persist.
17+
*/
18+
save(token:Token):Promise<void>;
1219
}
Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import { User } from '../Models/Persistents/User'
22

3+
/**
4+
* Represents an user repository to persists users.
5+
*/
36
export interface IUserRepository {
4-
5-
getAll():Promise<Array<User>>;
6-
7-
getList(name:string, limit:number, offset:number):Promise<Array<User>>;
87

9-
getById(id:string):Promise<User>;
10-
11-
save(client:User):Promise<void>;
8+
/**
9+
* Get an user instance by a given name.
10+
* @param username Represent the user name to look for.
11+
*/
12+
getByName(username:string):Promise<User>;
1213
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/**
2+
* Represents a token generation service to generate tokens.
3+
*/
4+
export interface ITokenGenerationService {
5+
6+
/**
7+
* Get a new ramdom token value;
8+
*/
9+
getTokenValue():Promise<string>;
10+
}

source/Services/ITokenService.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { Token } from '../Models/Persistents/Token'
2+
3+
/**
4+
* Represents a token service.
5+
*/
6+
export interface ITokenService {
7+
8+
/**
9+
* Get a token intance by the given value.
10+
* @param tokenValue Represents the token value to look for.
11+
*/
12+
getByValue(tokenValue:string):Promise<Token>;
13+
14+
/**
15+
* Get a new generated token.
16+
*/
17+
generateToken():Promise<Token>;
18+
}

source/Services/IUserService.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* Represent an user service.
3+
*/
4+
export interface IUserService {
5+
/**
6+
* Get a boolean value specifying if the user credential are valid.
7+
* @param username Represents the user name.
8+
* @param password Represents the user password.
9+
*/
10+
validateCredential(username:string, password:string):Promise<boolean>;
11+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { ITokenGenerationService } from './ITokenGenerationService'
2+
3+
/**
4+
* Represents a token generation service to generate tokens.
5+
*/
6+
export class TokenGenerationService implements ITokenGenerationService {
7+
8+
/**
9+
* Get a new ramdom token value;
10+
*/
11+
public async getTokenValue():Promise<string> {
12+
return 'sdhbfhksbdjkfbsbgbsjfkabghkbf';
13+
}
14+
}

0 commit comments

Comments
 (0)