diff options
author | 2019-03-25 14:27:10 +0000 | |
---|---|---|
committer | 2019-03-25 14:27:10 +0000 | |
commit | 1f68c64e8bcf88ee485c69d36c67b0ad6590a293 (patch) | |
tree | 34ab01010c3a8b3bb4086d4b683a6fd8040f2992 /sdnr/wt/odlux/framework/src/services/authenticationService.ts | |
parent | 528f6391de8495f3346b5e22ede404c00b9955b7 (diff) | |
parent | 2d4424c28ac35763ef44c42ae2f01664d42b268c (diff) |
Merge "Security provider for UX-Client-Login"
Diffstat (limited to 'sdnr/wt/odlux/framework/src/services/authenticationService.ts')
-rw-r--r-- | sdnr/wt/odlux/framework/src/services/authenticationService.ts | 36 |
1 files changed, 28 insertions, 8 deletions
diff --git a/sdnr/wt/odlux/framework/src/services/authenticationService.ts b/sdnr/wt/odlux/framework/src/services/authenticationService.ts index 5e6fc81a8..42ce061f1 100644 --- a/sdnr/wt/odlux/framework/src/services/authenticationService.ts +++ b/sdnr/wt/odlux/framework/src/services/authenticationService.ts @@ -1,14 +1,34 @@ -function timeout(ms:number) { - return new Promise(resolve => setTimeout(resolve, ms)); +import { requestRest, formEncode } from "./restService"; +import { AuthToken } from "../models/authentication"; + +type AuthTokenResponse = { + access_token: string; + token_type: string; + expires_in: number; } + class AuthenticationService { - public async authenticateUser(email: string, password: string) : Promise<string | null> { - await timeout(650); - if (email === "max@odlux.com" && password === "geheim") { - return "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJPRExVWCIsImlhdCI6MTUzODQ2NDMyMCwiZXhwIjoxNTcwMDAwMzIwLCJhdWQiOiJsb2NhbGhvc3QiLCJzdWIiOiJsb2NhbGhvc3QiLCJmaXJzdE5hbWUiOiJNYXgiLCJsYXN0TmFtZSI6Ik11c3Rlcm1hbm4iLCJlbWFpbCI6Im1heEBvZGx1eC5jb20iLCJyb2xlIjpbInVzZXIiLCJhZG1pbiJdfQ.9e5hDi2uxmIXNwHkJoScBZsHBk0jQ8CcZ7YIcZhDtuI" - } - return null; + public async authenticateUser(email: string, password: string, scope: string): Promise<AuthToken | null> { + const result = await requestRest<string>(`oauth2/token`, { + method: "POST", + headers: { + 'Content-Type': 'application/x-www-form-urlencoded' + }, + body: formEncode({ + grant_type: "password", + username: email, + password: password, + scope: scope + }) + }, false); + const resultObj: AuthTokenResponse| null = result && JSON.parse(result); + return resultObj && { + username: email, + access_token: resultObj.access_token, + token_type: resultObj.token_type, + expires: (new Date().valueOf()) + (resultObj.expires_in * 1000) + } || null; } } |