001/*
002 * (C) Copyright 2015 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl-2.1.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     François Maturel
016 */
017
018package org.nuxeo.ecm.platform.ui.web.keycloak;
019
020import java.util.Set;
021
022import org.apache.commons.lang3.StringUtils;
023import org.nuxeo.ecm.platform.api.login.UserIdentificationInfo;
024
025/**
026 * @since 7.4
027 */
028public class KeycloakUserInfo extends UserIdentificationInfo {
029
030    private static final long serialVersionUID = 6894397878763275157L;
031
032    protected String firstName;
033
034    protected String lastName;
035
036    protected String company;
037
038    protected Set<String> roles;
039
040    private KeycloakUserInfo(String emailAsUserName, String password) {
041        super(emailAsUserName, password);
042    }
043
044    public KeycloakUserInfo(String emailAsUserName, String password, String firstName, String lastName, String company) {
045        super(emailAsUserName, password);
046
047        if (emailAsUserName == null || StringUtils.isEmpty(emailAsUserName)) {
048            throw new IllegalStateException("A valid username should always be provided");
049        }
050
051        this.firstName = firstName;
052        this.lastName = lastName;
053        this.company = company;
054    }
055
056    public String getFirstName() {
057        return firstName;
058    }
059
060    public String getLastName() {
061        return lastName;
062    }
063
064    public String getCompany() {
065        return company;
066    }
067
068    public Set<String> getRoles() {
069        return roles;
070    }
071
072    public void setRoles(Set<String> roles) {
073        this.roles = roles;
074    }
075
076    public static class KeycloakUserInfoBuilder {
077        protected String token;
078
079        protected String userName;
080
081        protected String password;
082
083        protected String authPluginName;
084
085        protected String company;
086
087        protected String lastName;
088
089        protected String firstName;
090
091        private KeycloakUserInfoBuilder() {
092        }
093
094        public static KeycloakUserInfoBuilder aKeycloakUserInfo() {
095            return new KeycloakUserInfoBuilder();
096        }
097
098        public KeycloakUserInfoBuilder withToken(String token) {
099            this.token = token;
100            return this;
101        }
102
103        public KeycloakUserInfoBuilder withUserName(String userName) {
104            this.userName = userName;
105            return this;
106        }
107
108        public KeycloakUserInfoBuilder withPassword(String password) {
109            this.password = password;
110            return this;
111        }
112
113        public KeycloakUserInfoBuilder withAuthPluginName(String authPluginName) {
114            this.authPluginName = authPluginName;
115            return this;
116        }
117
118        public KeycloakUserInfoBuilder withCompany(String company) {
119            this.company = company;
120            return this;
121        }
122
123        public KeycloakUserInfoBuilder withLastName(String lastName) {
124            this.lastName = lastName;
125            return this;
126        }
127
128        public KeycloakUserInfoBuilder withFirstName(String firstName) {
129            this.firstName = firstName;
130            return this;
131        }
132
133        public KeycloakUserInfo build() {
134            KeycloakUserInfo keycloakUserInfo = new KeycloakUserInfo(userName, password, firstName, lastName, company);
135            keycloakUserInfo.setToken(token);
136            keycloakUserInfo.setAuthPluginName(authPluginName);
137            return keycloakUserInfo;
138        }
139    }
140}