1 |
- {"version":3,"sources":["../../src/app/account-service/account.service.ts"],"names":["ContentAlert","AccountServiceError","Error","constructor","response","statusText","AccountServiceClient","keycloakService","kcSvc","accountUrl","authServerUrl","realm","doGet","endpoint","config","doRequest","method","doDelete","doPost","body","JSON","stringify","doPut","fetch","makeUrl","toString","makeConfig","data","json","e","ok","handleError","status","authenticated","audiencePresent","window","location","href","baseUrl","login","err","danger","startsWith","URL","url","hasOwnProperty","params","Object","keys","forEach","key","searchParams","append","Promise","resolve","getToken","then","token","headers","Authorization","catch","addEventListener","event","promise","error","preventDefault"],"mappings":";;AAAA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AAGA,SAAQA,YAAR;AAcA,OAAO,MAAMC,mBAAN,SAAkCC,KAAlC,CAAwC;AAC3CC,EAAAA,WAAW,CAAQC,QAAR,EAAgC;AACvC,UAAMA,QAAQ,CAACC,UAAf;AADuC,SAAxBD,QAAwB,GAAxBA,QAAwB;AAE1C;;AAH0C;AAM/C;AACA;AACA;AACA;;AACA,OAAO,MAAME,oBAAN,CAA2B;AAIvBH,EAAAA,WAAW,CAACI,eAAD,EAAmC;AAAA;;AAAA;;AACjD,SAAKC,KAAL,GAAaD,eAAb;AACA,SAAKE,UAAL,GAAkB,KAAKD,KAAL,CAAWE,aAAX,KAA6B,SAA7B,GAAyC,KAAKF,KAAL,CAAWG,KAAX,EAAzC,GAA8D,UAAhF;AACH;;AAEiB,QAALC,KAAK,CAAIC,QAAJ,EACIC,MADJ,EAC8D;AAC5E,WAAO,KAAKC,SAAL,CAAeF,QAAf,EAAyB,EAAC,GAAGC,MAAJ;AAAYE,MAAAA,MAAM,EAAE;AAApB,KAAzB,CAAP;AACH;;AAEoB,QAARC,QAAQ,CAAIJ,QAAJ,EACGC,MADH,EAC6D;AAC9E,WAAO,KAAKC,SAAL,CAAeF,QAAf,EAAyB,EAAC,GAAGC,MAAJ;AAAYE,MAAAA,MAAM,EAAE;AAAp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AAR,CAAP;AACjC,UAAM+B,GAAG,GAAG,IAAID,GAAJ,CAAQ,KAAKlC,UAAL,GAAkBI,QAA1B,CAAZ,CAFmE,CAInE;;AACA,QAAIC,MAAM,IAAIA,MAAM,CAAC+B,cAAP,CAAsB,QAAtB,CAAd,EAA+C;AAC3C,YAAMC,MAAgC,GAAGhC,MAAM,CAACgC,MAAP,IAAuB,EAAhE;AACAC,MAAAA,MAAM,CAACC,IAAP,CAAYF,MAAZ,EAAoBG,OAApB,CAA4BC,GAAG,IAAIN,GAAG,CAACO,YAAJ,CAAiBC,MAAjB,CAAwBF,GAAxB,EAA6BJ,MAAM,CAACI,GAAD,CAAnC,CAAnC;AACH;;AAED,WAAON,GAAP;AACH;;AAEOlB,EAAAA,UAAU,CAACZ,MAAmB,GAAG,EAAvB,EAAiD;AAC/D,WAAO,IAAIuC,OAAJ,CAAcC,OAAD,IAA4B;AAC5C,WAAK9C,KAAL,CAAW+C,QAAX,GACKC,IADL,CACYC,KAAD,IAAmB;AACtBH,QAAAA,OAAO,CAAE,EACL,GAAGxC,MADE;AAEL4C,UAAAA,OAAO,EAAE;AAAC,4BAAgB,kBAAjB;AACA,eAAG5C,MAAM,CAAC4C,OADV;AAECC,YAAAA,aAAa,EAAE,YAAYF;AAF5B;AAFJ,SAAF,CAAP;AAMH,OARL,EAQOG,KARP,CAQa,MAAM;AACX,aAAKpD,KAAL,CAAW+B,KAAX;AACH,OAVL;AAWH,KAZM,CAAP;AAaH;;AAxG6B;AA4GlCJ,MAAM,CAAC0B,gBAAP,CAAwB,oBAAxB,EAA+CC,KAAD,IAAkC;AAC5EA,EAAAA,KAAK,CAACC,OAAN,CAAcH,KAAd,CAAoBI,KAAK,IAAI;AACzB,QAAIA,KAAK,YAAY/D,mBAArB,EAA0C;AACtC;AACA6D,MAAAA,KAAK,CAACG,cAAN;AACH;AACJ,GALD;AAMH,CAPD","sourcesContent":["/*\n * Copyright 2018 Red Hat Inc. and/or its affiliates and other contributors\n * as indicated by the @author tags. All rights reserved.\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\"); you may not\n * use this file except in compliance with the License. You may obtain a copy of\n * the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS, WITHOUT\n * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the\n * License for the specific language governing permissions and limitations under\n * the License.\n */\n\nimport {KeycloakService} from '../keycloak-service/keycloak.service';\nimport {ContentAlert} from '../content/ContentAlert';\n\ndeclare const baseUrl: string;\n\ntype ConfigResolve = (config: RequestInit) => void;\n\nexport interface HttpResponse<T = {}> extends Response {\n data?: T;\n}\n\nexport interface RequestInitWithParams extends RequestInit {\n params?: {[name: string]: string | number};\n}\n\nexport class AccountServiceError extends Error {\n constructor(public response: HttpResponse) {\n super(response.statusText);\n }\n}\n\n/**\n *\n * @author Stan Silvert ssilvert@redhat.com (C) 2018 Red Hat Inc.\n */\nexport class AccountServiceClient {\n private kcSvc: KeycloakService;\n private accountUrl: string;\n\n public constructor(keycloakService: KeycloakService) {\n this.kcSvc = keycloakService;\n this.accountUrl = this.kcSvc.authServerUrl() + 'realms/' + this.kcSvc.realm() + '/account';\n }\n\n public async doGet<T>(endpoint: string,\n config?: RequestInitWithParams): Promise<HttpResponse<T>> {\n return this.doRequest(endpoint, {...config, method: 'get'});\n }\n\n public async doDelete<T>(endpoint: string,\n config?: RequestInitWithParams): Promise<HttpResponse<T>> {\n return this.doRequest(endpoint, {...config, method: 'delete'});\n }\n\n public async doPost<T>(endpoint: string,\n body: string | {},\n config?: RequestInitWithParams): Promise<HttpResponse<T>> {\n return this.doRequest(endpoint, {...config, body: JSON.stringify(body), method: 'post'});\n }\n\n public async doPut<T>(endpoint: string,\n body: string | {},\n config?: RequestInitWithParams): Promise<HttpResponse<T>> {\n return this.doRequest(endpoint, {...config, body: JSON.stringify(body), method: 'put'});\n }\n\n public async doRequest<T>(endpoint: string,\n config?: RequestInitWithParams): Promise<HttpResponse<T>> {\n\n const response: HttpResponse<T> = await fetch(this.makeUrl(endpoint, config).toString(),\n await this.makeConfig(config));\n\n try {\n response.data = await response.json();\n } catch (e) {} // ignore. Might be empty\n\n if (!response.ok) {\n this.handleError(response);\n throw new AccountServiceError(response);\n }\n\n return response;\n }\n\n private handleError(response: HttpResponse): void {\n if (response !== null && response.status === 401) {\n if (this.kcSvc.authenticated() && !this.kcSvc.audiencePresent()) {\n // authenticated and the audience is not present => not allowed\n window.location.href = baseUrl + '#/forbidden';\n } else {\n // session timed out?\n this.kcSvc.login();\n }\n }\n\n if (response !== null && response.status === 403) {\n window.location.href = baseUrl + '#/forbidden';\n }\n\n if (response !== null && response.data != null) {\n if (response.data['errors'] != null) {\n for(let err of response.data['errors'])\n ContentAlert.danger(err['errorMessage'], err['params']);\n } else {\n ContentAlert.danger(\n `${response.statusText}: ${response.data['errorMessage'] ? response.data['errorMessage'] : ''} ${response.data['error'] ? response.data['error'] : ''}`);\n };\n } else {\n ContentAlert.danger(response.statusText);\n }\n }\n\n private makeUrl(endpoint: string, config?: RequestInitWithParams): URL {\n if (endpoint.startsWith('http')) return new URL(endpoint);\n const url = new URL(this.accountUrl + endpoint);\n\n // add request params\n if (config && config.hasOwnProperty('params')) {\n const params: {[name: string]: string} = config.params as {} || {};\n Object.keys(params).forEach(key => url.searchParams.append(key, params[key]))\n }\n\n return url;\n }\n\n private makeConfig(config: RequestInit = {}): Promise<RequestInit> {\n return new Promise( (resolve: ConfigResolve) => {\n this.kcSvc.getToken()\n .then( (token: string) => {\n resolve( {\n ...config,\n headers: {'Content-Type': 'application/json',\n ...config.headers,\n Authorization: 'Bearer ' + token}\n });\n }).catch(() => {\n this.kcSvc.login();\n });\n });\n }\n\n}\n\nwindow.addEventListener(\"unhandledrejection\", (event: PromiseRejectionEvent) => {\n event.promise.catch(error => {\n if (error instanceof AccountServiceError) {\n // We already handled the error. Ignore unhandled rejection.\n event.preventDefault();\n }\n });\n});\n"],"file":"account.service.js"}
|