@christianriedl/rest 1.0.15 → 1.0.18
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/authorize.d.ts +36 -0
- package/dist/authorize.js +85 -0
- package/dist/authorize.js.map +1 -0
- package/dist/iRest.d.ts +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/oauth.js +1 -1
- package/dist/rest.js +1 -1
- package/package.json +5 -5
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { ILocalStorage, ILogger, IAppConfig, Dictionary } from '@christianriedl/utils';
|
|
2
|
+
import { Rest } from './rest';
|
|
3
|
+
export interface IAuthorizeResult {
|
|
4
|
+
mediaSecret: string;
|
|
5
|
+
smarthomeSecret: string;
|
|
6
|
+
scopes: string;
|
|
7
|
+
user: string;
|
|
8
|
+
password: string;
|
|
9
|
+
}
|
|
10
|
+
export interface IMeta {
|
|
11
|
+
scopes: string[];
|
|
12
|
+
}
|
|
13
|
+
export interface IRoute {
|
|
14
|
+
path: string;
|
|
15
|
+
meta?: IMeta;
|
|
16
|
+
}
|
|
17
|
+
export declare class Authorize {
|
|
18
|
+
rest: Rest;
|
|
19
|
+
storage: ILocalStorage;
|
|
20
|
+
secretSmarthome: string;
|
|
21
|
+
secretMedia: string;
|
|
22
|
+
scopes: Dictionary<boolean>;
|
|
23
|
+
routes: Dictionary<IRoute>;
|
|
24
|
+
password: string;
|
|
25
|
+
config: IAppConfig;
|
|
26
|
+
get user(): string;
|
|
27
|
+
set user(value: string);
|
|
28
|
+
constructor(url: string, storage: ILocalStorage, config: IAppConfig, logger: ILogger);
|
|
29
|
+
setScopes(scopes: string): void;
|
|
30
|
+
setRoutes(routes: IRoute[]): void;
|
|
31
|
+
checkScope(scopes: string[]): boolean;
|
|
32
|
+
checkRoute(path: string): boolean;
|
|
33
|
+
authorize(user: string, password: string): Promise<IAuthorizeResult>;
|
|
34
|
+
login(user: string, password: string): Promise<IAuthorizeResult>;
|
|
35
|
+
processResult(res: IAuthorizeResult, all: boolean): void;
|
|
36
|
+
}
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
import { Rest } from './rest';
|
|
2
|
+
import { DummyOAuth } from './oauth';
|
|
3
|
+
export class Authorize {
|
|
4
|
+
rest;
|
|
5
|
+
storage;
|
|
6
|
+
secretSmarthome;
|
|
7
|
+
secretMedia;
|
|
8
|
+
scopes = {};
|
|
9
|
+
routes = {};
|
|
10
|
+
password;
|
|
11
|
+
config;
|
|
12
|
+
get user() {
|
|
13
|
+
return this.config.user;
|
|
14
|
+
}
|
|
15
|
+
set user(value) {
|
|
16
|
+
this.config.user = value;
|
|
17
|
+
}
|
|
18
|
+
constructor(url, storage, config, logger) {
|
|
19
|
+
this.rest = new Rest(url, 'same-origin', new DummyOAuth(), logger);
|
|
20
|
+
this.storage = storage;
|
|
21
|
+
this.config = config;
|
|
22
|
+
this.secretMedia = storage.getItemString('secretMedia', '');
|
|
23
|
+
this.secretSmarthome = storage.getItemString('secretSmarthome', '');
|
|
24
|
+
this.password = storage.getItemString('password', '');
|
|
25
|
+
const scopes = storage.getItemString('scopes');
|
|
26
|
+
if (scopes)
|
|
27
|
+
this.setScopes(scopes);
|
|
28
|
+
}
|
|
29
|
+
setScopes(scopes) {
|
|
30
|
+
const splitScopes = scopes.split('|');
|
|
31
|
+
for (let i = 0; i < splitScopes.length; i++)
|
|
32
|
+
this.scopes[splitScopes[i]] = true;
|
|
33
|
+
}
|
|
34
|
+
setRoutes(routes) {
|
|
35
|
+
for (let i = 0; i < routes.length; i++) {
|
|
36
|
+
this.routes[routes[i].path] = routes[i];
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
checkScope(scopes) {
|
|
40
|
+
for (let i = 0; i < scopes.length; i++)
|
|
41
|
+
if (!this.scopes[scopes[i]])
|
|
42
|
+
return false;
|
|
43
|
+
return true;
|
|
44
|
+
}
|
|
45
|
+
checkRoute(path) {
|
|
46
|
+
const route = this.routes[path];
|
|
47
|
+
if (!route)
|
|
48
|
+
return false;
|
|
49
|
+
if (route.meta)
|
|
50
|
+
return this.checkScope(route.meta.scopes);
|
|
51
|
+
return true;
|
|
52
|
+
}
|
|
53
|
+
async authorize(user, password) {
|
|
54
|
+
const result = await this.rest.postDataEx("apiauth/authorize", { user: user, password: password });
|
|
55
|
+
if (result.ok && result.result) {
|
|
56
|
+
const res = result.result;
|
|
57
|
+
this.processResult(res, true);
|
|
58
|
+
return res;
|
|
59
|
+
}
|
|
60
|
+
return null;
|
|
61
|
+
}
|
|
62
|
+
async login(user, password) {
|
|
63
|
+
const result = await this.rest.postDataEx("apiauth/login", { user: user, password: password });
|
|
64
|
+
if (result.ok && result.result) {
|
|
65
|
+
const res = result.result;
|
|
66
|
+
this.processResult(res, false);
|
|
67
|
+
return res;
|
|
68
|
+
}
|
|
69
|
+
return null;
|
|
70
|
+
}
|
|
71
|
+
processResult(res, all) {
|
|
72
|
+
this.storage.setItem('scopes', res.scopes);
|
|
73
|
+
this.setScopes(res.scopes);
|
|
74
|
+
if (all) {
|
|
75
|
+
this.user = res.user;
|
|
76
|
+
this.password = res.password;
|
|
77
|
+
this.secretMedia = res.mediaSecret;
|
|
78
|
+
this.secretSmarthome = res.mediaSecret;
|
|
79
|
+
this.storage.setItem('password', res.password);
|
|
80
|
+
this.storage.setItem('secretMedia', res.mediaSecret);
|
|
81
|
+
this.storage.setItem('secretSmarthome', res.smarthomeSecret);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
//# sourceMappingURL=authorize.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"authorize.js","sourceRoot":"","sources":["../src/authorize.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,IAAI,EAAE,MAAM,QAAQ,CAAC;AAC9B,OAAO,EAAE,UAAU,EAAE,MAAM,SAAS,CAAC;AAoBrC,MAAM,OAAO,SAAS;IAClB,IAAI,CAAO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: IProblemResult | T | IValueResult<T>;
|
|
20
20
|
}
|
|
21
21
|
export interface IRest {
|
|
22
|
+
serviceUrl: string;
|
|
22
23
|
getData<T>(path: string, queryParameter?: Dictionary<any>, adjustResult?: (result: IRestResult<T>) => void): Promise<IRestResult<T>>;
|
|
23
24
|
postData<T>(path: string, data?: T): Promise<IRestResult<boolean>>;
|
|
24
25
|
postDataEx<T, R>(path: string, data?: T, adjustResult?: (result: IRestResult<R>) => void): Promise<IRestResult<R>>;
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,QAAQ,CAAC;AACvB,cAAc,SAAS,CAAC"}
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,QAAQ,CAAC;AACvB,cAAc,SAAS,CAAC;AACxB,cAAc,aAAa,CAAC"}
|
package/dist/oauth.js
CHANGED
|
@@ -65,7 +65,7 @@ export class OAuth {
|
|
|
65
65
|
const expiresAt = this.localStorage.getItemNumber('expiresAt');
|
|
66
66
|
if (expiresAt > 0) {
|
|
67
67
|
this.expiresAt = expiresAt;
|
|
68
|
-
this.log.trace(`
|
|
68
|
+
this.log.trace(`Stored ${this.clientId} expiresAt : ${new Date(expiresAt)}`);
|
|
69
69
|
if (this.isExpired) {
|
|
70
70
|
this.log.trace(`${this.clientId} already expired`);
|
|
71
71
|
}
|
package/dist/rest.js
CHANGED
|
@@ -225,7 +225,7 @@ export class Rest {
|
|
|
225
225
|
getBlob(path, mediaType, queryParameter) {
|
|
226
226
|
const self = this;
|
|
227
227
|
const url = Rest.buildUrl(this.serviceUrl + path, queryParameter);
|
|
228
|
-
this.log.trace("
|
|
228
|
+
this.log.trace("getBlob : " + url);
|
|
229
229
|
const promise = new Promise(function (resolve, reject) {
|
|
230
230
|
self.auth.getToken()
|
|
231
231
|
.then((token) => {
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@christianriedl/rest",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.18",
|
|
4
4
|
"description": "REST and OAuth library based on fetch API",
|
|
5
5
|
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -17,11 +17,11 @@
|
|
|
17
17
|
"author": "Christian Riedl",
|
|
18
18
|
"license": "ISC",
|
|
19
19
|
"dependencies": {
|
|
20
|
-
"@christianriedl/utils": "^1.0.
|
|
20
|
+
"@christianriedl/utils": "^1.0.51"
|
|
21
21
|
},
|
|
22
22
|
"devDependencies": {
|
|
23
|
-
"typescript": "^4.
|
|
24
|
-
"@types/node": "^17.0.
|
|
25
|
-
"@babel/types": "7.
|
|
23
|
+
"typescript": "^4.6.3",
|
|
24
|
+
"@types/node": "^17.0.25",
|
|
25
|
+
"@babel/types": "^7.17.0"
|
|
26
26
|
}
|
|
27
27
|
}
|