9dd2fd4f7aefe25fa6de0fe0a7c0265e16f46614
[moon.git] /
1 /*
2  * Copyright (c) 2015 Brocade Communications Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.aaa.shiro.moon;
9
10 import com.google.common.collect.ImmutableSet;
11
12 import java.io.Serializable;
13 import java.util.Set;
14
15 import org.opendaylight.aaa.api.Claim;
16
17 /**
18  * MoonPrincipal contains all user's information returned by moon on successful authentication
19  * @author Alioune BA alioune.ba@orange.com
20  *
21  */
22 public  class MoonPrincipal {
23
24     private final String username;
25     private final String domain;
26     private final String userId;
27     private final Set<String> roles;
28     private final String token;
29
30
31     public MoonPrincipal(String username, String domain, String userId, Set<String> roles, String token) {
32         this.username = username;
33         this.domain = domain;
34         this.userId = userId;
35         this.roles = roles;
36         this.token = token;
37     }
38
39     public MoonPrincipal createODLPrincipal(String username, String domain,
40             String userId, Set<String> roles, String token) {
41
42         return new MoonPrincipal(username, domain, userId, roles,token);
43     }
44
45     public Claim principalToClaim (){
46         return new MoonClaim("", this.getUserId(), this.getUsername(), this.getDomain(), this.getRoles());
47     }
48
49     public String getUsername() {
50         return this.username;
51     }
52
53     public String getDomain() {
54         return this.domain;
55     }
56
57     public String getUserId() {
58         return this.userId;
59     }
60
61     public Set<String> getRoles() {
62         return this.roles;
63     }
64
65     public String getToken(){
66         return this.token;
67     }
68
69     public class MoonClaim implements Claim, Serializable {
70         private static final long serialVersionUID = -8115027645190209125L;
71         private int hashCode = 0;
72         private String clientId;
73         private String userId;
74         private String user;
75         private String domain;
76         private ImmutableSet<String> roles;
77
78         public MoonClaim(String clientId, String userId, String user, String domain, Set<String> roles) {
79             this.clientId = clientId;
80             this.userId = userId;
81             this.user = user;
82             this.domain = domain;
83             this.roles = ImmutableSet.<String> builder().addAll(roles).build();
84
85             if (userId.isEmpty() || user.isEmpty() || roles.isEmpty() || roles.contains("")) {
86                 throw new IllegalStateException("The Claim is missing one or more of the required fields.");
87             }
88         }
89
90         @Override
91         public String clientId() {
92             return clientId;
93         }
94
95         @Override
96         public String userId() {
97             return userId;
98         }
99
100         @Override
101         public String user() {
102             return user;
103         }
104
105         @Override
106         public String domain() {
107             return domain;
108         }
109
110         @Override
111         public Set<String> roles() {
112             return roles;
113         }
114         public String getClientId() {
115             return clientId;
116         }
117
118         public void setClientId(String clientId) {
119             this.clientId = clientId;
120         }
121
122         public String getUserId() {
123             return userId;
124         }
125
126         public void setUserId(String userId) {
127             this.userId = userId;
128         }
129
130         public String getUser() {
131             return user;
132         }
133
134         public void setUser(String user) {
135             this.user = user;
136         }
137
138         public String getDomain() {
139             return domain;
140         }
141
142         public void setDomain(String domain) {
143             this.domain = domain;
144         }
145
146         public ImmutableSet<String> getRoles() {
147             return roles;
148         }
149
150         public void setRoles(ImmutableSet<String> roles) {
151             this.roles = roles;
152         }
153
154         @Override
155         public String toString() {
156             return "clientId:" + clientId + "," + "userId:" + userId + "," + "userName:" + user
157                     + "," + "domain:" + domain + "," + "roles:" + roles ;
158         }
159     }
160 }