summaryrefslogtreecommitdiffstats
path: root/sdnr/wt/odlux/framework/src/services/authenticationService.ts
diff options
context:
space:
mode:
Diffstat (limited to 'sdnr/wt/odlux/framework/src/services/authenticationService.ts')
-rw-r--r--sdnr/wt/odlux/framework/src/services/authenticationService.ts36
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;
}
}