@christianriedl/rest 1.0.15 → 1.0.16

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.
@@ -0,0 +1,25 @@
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 declare class Authorize {
11
+ rest: Rest;
12
+ storage: ILocalStorage;
13
+ secretSmarthome: string;
14
+ secretMedia: string;
15
+ scopes: Dictionary<boolean>;
16
+ password: string;
17
+ config: IAppConfig;
18
+ get user(): string;
19
+ set user(value: string);
20
+ constructor(url: string, storage: ILocalStorage, config: IAppConfig, logger: ILogger);
21
+ setRoutes(routes: string): void;
22
+ authorize(user: string, password: string): Promise<IAuthorizeResult>;
23
+ login(user: string, password: string): Promise<IAuthorizeResult>;
24
+ processResult(res: IAuthorizeResult, all: boolean): void;
25
+ }
@@ -0,0 +1,64 @@
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
+ password;
10
+ config;
11
+ get user() {
12
+ return this.config.user;
13
+ }
14
+ set user(value) {
15
+ this.config.user = value;
16
+ }
17
+ constructor(url, storage, config, logger) {
18
+ this.rest = new Rest(url, 'same-origin', new DummyOAuth(), logger);
19
+ this.storage = storage;
20
+ this.config = config;
21
+ this.secretMedia = storage.getItemString('secretMedia', '');
22
+ this.secretSmarthome = storage.getItemString('secretSmarthome', '');
23
+ this.password = storage.getItemString('password', '');
24
+ const routes = storage.getItemString('allowedRoutes', '/authorize');
25
+ if (routes)
26
+ this.setRoutes(routes);
27
+ }
28
+ setRoutes(routes) {
29
+ const splitRoutes = routes.split('|');
30
+ for (let i = 0; i < splitRoutes.length; i++)
31
+ this.scopes[splitRoutes[i]] = true;
32
+ }
33
+ async authorize(user, password) {
34
+ const result = await this.rest.postDataEx("apiauth/authorize", { user: user, password: password });
35
+ if (result.ok && result.result) {
36
+ const res = result.result;
37
+ this.processResult(res, true);
38
+ return res;
39
+ }
40
+ return null;
41
+ }
42
+ async login(user, password) {
43
+ const result = await this.rest.postDataEx("apiauth/login", { user: user, password: password });
44
+ if (result.ok && result.result) {
45
+ const res = result.result;
46
+ this.processResult(res, false);
47
+ return res;
48
+ }
49
+ return null;
50
+ }
51
+ processResult(res, all) {
52
+ this.storage.setItem('allowedRoutes', res.scopes);
53
+ this.setRoutes(res.scopes);
54
+ if (all) {
55
+ this.password = res.password;
56
+ this.secretMedia = res.mediaSecret;
57
+ this.secretSmarthome = res.mediaSecret;
58
+ this.storage.setItem('password', res.password);
59
+ this.storage.setItem('secretMedia', res.mediaSecret);
60
+ this.storage.setItem('secretSmarthome', res.smarthomeSecret);
61
+ }
62
+ }
63
+ }
64
+ //# 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;AAcrC,MAAM,OAAO,SAAS;IAClB,IAAI,CAAO;IACX,OAAO,CAAgB;IACvB,eAAe,CAAS;IACxB,WAAW,CAAS;IACpB,MAAM,GAAwB,EAAE,CAAC;IACjC,QAAQ,CAAS;IACjB,MAAM,CAAa;IACnB,IAAI,IAAI;QACJ,OAAO,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC;IAC5B,CAAC;IACD,IAAI,IAAI,CAAC,KAAa;QAClB,IAAI,CAAC,MAAM,CAAC,IAAI,GAAG,KAAK,CAAC;IAC7B,CAAC;IACD,YAAY,GAAW,EAAE,OAAsB,EAAE,MAAkB,EAAE,MAAe;QAChF,IAAI,CAAC,IA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| 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
@@ -1,2 +1,3 @@
1
1
  export * from './rest';
2
2
  export * from './oauth';
3
+ export * from './authorize';
package/dist/index.js CHANGED
@@ -1,3 +1,4 @@
1
1
  export * from './rest';
2
2
  export * from './oauth';
3
+ export * from './authorize';
3
4
  //# sourceMappingURL=index.js.map
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(`stored ${this.clientId} expiresAt : ${new Date(expiresAt)}`);
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("getData : " + url);
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.15",
3
+ "version": "1.0.16",
4
4
  "description": "REST and OAuth library based on fetch API",
5
5
 
6
6
  "main": "dist/index.js",
@@ -17,7 +17,7 @@
17
17
  "author": "Christian Riedl",
18
18
  "license": "ISC",
19
19
  "dependencies": {
20
- "@christianriedl/utils": "^1.0.24"
20
+ "@christianriedl/utils": "^1.0.50"
21
21
  },
22
22
  "devDependencies": {
23
23
  "typescript": "^4.5.4",