001/*
002 * (C) Copyright 2015 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     François Maturel
018 */
019
020package org.nuxeo.ecm.platform.ui.web.keycloak;
021
022import java.util.Set;
023
024import org.apache.commons.lang3.StringUtils;
025import org.nuxeo.ecm.platform.api.login.UserIdentificationInfo;
026
027/**
028 * @since 7.4
029 */
030public class KeycloakUserInfo extends UserIdentificationInfo {
031
032    private static final long serialVersionUID = 6894397878763275157L;
033
034    protected String firstName;
035
036    protected String lastName;
037
038    protected String company;
039
040    protected Set<String> roles;
041
042    public KeycloakUserInfo(String emailAsUserName, String password, String firstName, String lastName, String company) {
043        super(emailAsUserName, password);
044
045        if (emailAsUserName == null || StringUtils.isEmpty(emailAsUserName)) {
046            throw new IllegalStateException("A valid username should always be provided");
047        }
048
049        this.firstName = firstName;
050        this.lastName = lastName;
051        this.company = company;
052    }
053
054    public String getFirstName() {
055        return firstName;
056    }
057
058    public String getLastName() {
059        return lastName;
060    }
061
062    public String getCompany() {
063        return company;
064    }
065
066    public Set<String> getRoles() {
067        return roles;
068    }
069
070    public void setRoles(Set<String> roles) {
071        this.roles = roles;
072    }
073
074    public static class KeycloakUserInfoBuilder {
075        protected String token;
076
077        protected String userName;
078
079        protected String password;
080
081        protected String company;
082
083        protected String lastName;
084
085        protected String firstName;
086
087        private KeycloakUserInfoBuilder() {
088        }
089
090        public static KeycloakUserInfoBuilder aKeycloakUserInfo() {
091            return new KeycloakUserInfoBuilder();
092        }
093
094        public KeycloakUserInfoBuilder withToken(String token) {
095            this.token = token;
096            return this;
097        }
098
099        public KeycloakUserInfoBuilder withUserName(String userName) {
100            this.userName = userName;
101            return this;
102        }
103
104        public KeycloakUserInfoBuilder withPassword(String password) {
105            this.password = password;
106            return this;
107        }
108
109        public KeycloakUserInfoBuilder withCompany(String company) {
110            this.company = company;
111            return this;
112        }
113
114        public KeycloakUserInfoBuilder withLastName(String lastName) {
115            this.lastName = lastName;
116            return this;
117        }
118
119        public KeycloakUserInfoBuilder withFirstName(String firstName) {
120            this.firstName = firstName;
121            return this;
122        }
123
124        public KeycloakUserInfo build() {
125            KeycloakUserInfo keycloakUserInfo = new KeycloakUserInfo(userName, password, firstName, lastName, company);
126            keycloakUserInfo.setCredentialsChecked(true);
127            keycloakUserInfo.setToken(token);
128            return keycloakUserInfo;
129        }
130    }
131}