mirror of https://github.com/jacekkow/keycloak-protocol-cas

Matthias Piepkorn
2018-01-22 1293d5189bfdc7c46fc456f52126b81febf23df8
commit | author | age
7f7e0c 1 package org.keycloak.protocol.cas;
MP 2
3 import org.jboss.logging.Logger;
4 import org.keycloak.events.EventBuilder;
5 import org.keycloak.models.*;
6 import org.keycloak.protocol.AbstractLoginProtocolFactory;
7 import org.keycloak.protocol.LoginProtocol;
8 import org.keycloak.protocol.ProtocolMapperUtils;
9 import org.keycloak.protocol.cas.mappers.FullNameMapper;
10 import org.keycloak.protocol.cas.mappers.UserAttributeMapper;
11 import org.keycloak.protocol.cas.mappers.UserPropertyMapper;
12 import org.keycloak.representations.idm.ClientRepresentation;
13 import org.keycloak.representations.idm.ClientTemplateRepresentation;
14
15 import java.util.ArrayList;
16 import java.util.HashMap;
17 import java.util.List;
18 import java.util.Map;
19
20 import static org.keycloak.protocol.oidc.mappers.OIDCAttributeMapperHelper.JSON_TYPE;
21 import static org.keycloak.protocol.oidc.mappers.OIDCAttributeMapperHelper.TOKEN_CLAIM_NAME;
22
23 public class CASLoginProtocolFactory extends AbstractLoginProtocolFactory {
24     private static final Logger logger = Logger.getLogger(CASLoginProtocolFactory.class);
25
26     public static final String EMAIL = "email";
27     public static final String EMAIL_VERIFIED = "email verified";
28     public static final String GIVEN_NAME = "given name";
29     public static final String FAMILY_NAME = "family name";
30     public static final String FULL_NAME = "full name";
31     public static final String LOCALE = "locale";
32
33     public static final String EMAIL_CONSENT_TEXT = "${email}";
34     public static final String EMAIL_VERIFIED_CONSENT_TEXT = "${emailVerified}";
35     public static final String GIVEN_NAME_CONSENT_TEXT = "${givenName}";
36     public static final String FAMILY_NAME_CONSENT_TEXT = "${familyName}";
37     public static final String FULL_NAME_CONSENT_TEXT = "${fullName}";
38     public static final String LOCALE_CONSENT_TEXT = "${locale}";
39
40     @Override
41     public LoginProtocol create(KeycloakSession session) {
42         return new CASLoginProtocol().setSession(session);
43     }
44
45     @Override
46     public List<ProtocolMapperModel> getBuiltinMappers() {
47         return builtins;
48     }
49
50     @Override
51     public List<ProtocolMapperModel> getDefaultBuiltinMappers() {
52         return defaultBuiltins;
53     }
54
55     static List<ProtocolMapperModel> builtins = new ArrayList<>();
56     static List<ProtocolMapperModel> defaultBuiltins = new ArrayList<>();
57
58     static {
59         ProtocolMapperModel model;
60
61         model = UserPropertyMapper.create(EMAIL, "email", "mail", "String",
62                 true, EMAIL_CONSENT_TEXT);
63         builtins.add(model);
64         defaultBuiltins.add(model);
65         model = UserPropertyMapper.create(GIVEN_NAME, "firstName", "givenName", "String",
66                 true, GIVEN_NAME_CONSENT_TEXT);
67         builtins.add(model);
68         defaultBuiltins.add(model);
69         model = UserPropertyMapper.create(FAMILY_NAME, "lastName", "sn", "String",
70                 true, FAMILY_NAME_CONSENT_TEXT);
71         builtins.add(model);
72         defaultBuiltins.add(model);
73         model = UserPropertyMapper.create(EMAIL_VERIFIED,
74                 "emailVerified",
75                 "emailVerified", "boolean",
76                 false, EMAIL_VERIFIED_CONSENT_TEXT);
77         builtins.add(model);
78         model = UserAttributeMapper.create(LOCALE,
79                 "locale",
80                 "locale", "String",
81                 false, LOCALE_CONSENT_TEXT,
82                 false);
83         builtins.add(model);
84
85         model = FullNameMapper.create(FULL_NAME, "cn",
86                 true, FULL_NAME_CONSENT_TEXT);
87         builtins.add(model);
88         defaultBuiltins.add(model);
89     }
90
91     @Override
92     protected void addDefaults(ClientModel client) {
93         for (ProtocolMapperModel model : defaultBuiltins) client.addProtocolMapper(model);
94     }
95
96     @Override
97     public Object createProtocolEndpoint(RealmModel realm, EventBuilder event) {
98         return new CASLoginProtocolService(realm, event);
99     }
100
101     @Override
102     public String getId() {
103         return CASLoginProtocol.LOGIN_PROTOCOL;
104     }
105
106     @Override
107     public void setupClientDefaults(ClientRepresentation rep, ClientModel newClient) {
108         if (rep.getRootUrl() != null && (rep.getRedirectUris() == null || rep.getRedirectUris().isEmpty())) {
109             String root = rep.getRootUrl();
110             if (root.endsWith("/")) root = root + "*";
111             else root = root + "/*";
112             newClient.addRedirectUri(root);
113         }
114
115         if (rep.getAdminUrl() == null && rep.getRootUrl() != null) {
116             newClient.setManagementUrl(rep.getRootUrl());
117         }
118     }
119
120     @Override
121     public void setupTemplateDefaults(ClientTemplateRepresentation clientRep, ClientTemplateModel newClient) {
122
123     }
124 }