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

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