keycloak.service.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
  2. /*
  3. * Copyright 2017 Red Hat, Inc. and/or its affiliates
  4. * and other contributors as indicated by the @author tags.
  5. *
  6. * Licensed under the Apache License, Version 2.0 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. export class KeycloakService {
  19. constructor(keycloak) {
  20. _defineProperty(this, "keycloakAuth", void 0);
  21. this.keycloakAuth = keycloak;
  22. }
  23. authenticated() {
  24. return this.keycloakAuth.authenticated ? this.keycloakAuth.authenticated : false;
  25. }
  26. audiencePresent() {
  27. if (this.keycloakAuth.tokenParsed) {
  28. const audience = this.keycloakAuth.tokenParsed['aud'];
  29. return audience === 'account' || Array.isArray(audience) && audience.indexOf('account') >= 0;
  30. }
  31. return false;
  32. }
  33. login(options) {
  34. this.keycloakAuth.login(options);
  35. }
  36. logout(redirectUri = baseUrl) {
  37. this.keycloakAuth.logout({
  38. redirectUri: redirectUri
  39. });
  40. }
  41. account() {
  42. this.keycloakAuth.accountManagement();
  43. }
  44. authServerUrl() {
  45. const authServerUrl = this.keycloakAuth.authServerUrl;
  46. return authServerUrl.charAt(authServerUrl.length - 1) === '/' ? authServerUrl : authServerUrl + '/';
  47. }
  48. realm() {
  49. return this.keycloakAuth.realm;
  50. }
  51. getToken() {
  52. return new Promise((resolve, reject) => {
  53. if (this.keycloakAuth.token) {
  54. this.keycloakAuth.updateToken(5).success(() => {
  55. resolve(this.keycloakAuth.token);
  56. }).error(() => {
  57. reject('Failed to refresh token');
  58. });
  59. } else {
  60. reject('Not logged in');
  61. }
  62. });
  63. }
  64. }
  65. //# sourceMappingURL=keycloak.service.js.map