1 import * as angular from 'angularAMD';
3 const security = angular.module('app.security');
5 interface IEnvService {
6 getBaseURL(port: string): string;
26 export class SecurityService {
27 private credentials: ng.IPromise<string>;
29 constructor(private $q: ng.IQService, private $http: ng.IHttpService, private $window, private env: IEnvService) {
30 this.ensureCrendentials();
33 private ensureCrendentials() {
34 const credentialsDefer = this.$q.defer<string>();
35 this.credentials = credentialsDefer.promise;
37 const url = `${this.env.getBaseURL('MD_SAL')}/oauth2/token`;
38 this.$http<{ access_token: string }>({
41 headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
42 data: `grant_type=password&username=${this.$window.sessionStorage.odlUser}&password=${this.$window.sessionStorage.odlPass}&scope=sdn`
44 credentialsDefer.resolve(res.data && res.data.access_token);
46 credentialsDefer.reject(err);
51 return this.credentials;
54 public getAllUsers(): ng.IPromise<User[]> {
55 const url = `${this.env.getBaseURL('MD_SAL')}/auth/v1/users`;
56 return this.token.then(token => {
57 return this.$http<{ users: User[] }>({
60 headers: { 'Authorization': `Bearer ${token}` }
61 }).then(result => result.data && result.data.users)
65 public getAllRoles(): ng.IPromise<Role[]> {
66 const url = `${this.env.getBaseURL('MD_SAL')}/auth/v1/roles`;
67 return this.token.then(token => {
68 return this.$http<{ roles: Role[] }>({
71 headers: { 'Authorization': `Bearer ${token}` }
72 }).then(result => result.data && result.data.roles)
76 public getUserById(userId: string): ng.IPromise<User> {
77 const url = `${this.env.getBaseURL('MD_SAL')}/auth/v1/users/${userId}`;
78 return this.token.then(token => {
79 return this.$http<User>({
82 headers: { 'Authorization': `Bearer ${token}` }
83 }).then(result => result.data && result.data)
87 public getRolesForDomainUser(userId: string, domain: string= "sdn"): ng.IPromise<Role[]> {
88 const url = `${this.env.getBaseURL('MD_SAL')}/auth/v1/domains/${domain}/users/${userId}/roles`;
89 return this.token.then(token => {
90 return this.$http<{ roles: Role[] }>({
93 headers: { 'Authorization': `Bearer ${token}` }
94 }).then(result => result.data && result.data.roles)
99 security.service('securityService', ['$q', '$http', '$window', 'ENV', SecurityService]);