001/*
002 * (C) Copyright 2014 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 *     Arnaud Kervern
018 */
019package org.nuxeo.ecm.platform.oauth2.openid.auth.github;
020
021import java.util.Date;
022
023import org.nuxeo.ecm.platform.oauth2.openid.auth.OpenIDUserInfo;
024
025import com.google.api.client.json.GenericJson;
026import com.google.api.client.util.DateTime;
027import com.google.api.client.util.Key;
028
029/**
030 * Github user info
031 *
032 * @author <a href="mailto:ak@nuxeo.com">Arnaud Kervern</a>
033 * @since 5.9.5
034 */
035public class GithubUserInfo extends GenericJson implements OpenIDUserInfo {
036
037    @Key("sub")
038    protected String subject;
039
040    @Key("name")
041    protected String name;
042
043    @Key("given_name")
044    protected String givenName;
045
046    @Key("family_name")
047    protected String familyName;
048
049    @Key("middle_name")
050    protected String middleName;
051
052    @Key("nickname")
053    protected String nickname;
054
055    @Key("preferred_username")
056    protected String preferredUsername;
057
058    @Key("profile")
059    protected String profile;
060
061    @Key("picture")
062    protected String picture;
063
064    @Key("website")
065    protected String website;
066
067    @Key("email")
068    protected String email;
069
070    @Key("email_verified")
071    protected boolean emailVerified;
072
073    @Key("gender")
074    protected String gender;
075
076    @Key("birthdate")
077    protected Date birthdate;
078
079    @Key("zoneinfo")
080    protected String zoneInfo;
081
082    @Key("locale")
083    protected String locale;
084
085    @Key("phone_number")
086    protected String phoneNumber;
087
088    @Key("address")
089    protected String address;
090
091    @Key("updated_time")
092    protected String updatedTime;
093
094    @Key
095    protected String bio;
096
097    @Key
098    protected String blog;
099
100    @Key
101    protected String company;
102
103    @Override
104    public String getSubject() {
105        return getEmail();
106    }
107
108    @Override
109    public String getName() {
110        return name;
111    }
112
113    @Override
114    public String getMiddleName() {
115        return middleName;
116    }
117
118    @Override
119    public String getPreferredUsername() {
120        return preferredUsername;
121    }
122
123    @Override
124    public String getProfile() {
125        return profile;
126    }
127
128    @Override
129    public String getPicture() {
130        return picture;
131    }
132
133    @Override
134    public String getWebsite() {
135        return website;
136    }
137
138    @Override
139    public String getEmail() {
140        return email;
141    }
142
143    @Override
144    public boolean isEmailVerified() {
145        return emailVerified;
146    }
147
148    @Override
149    public String getGender() {
150        return gender;
151    }
152
153    @Override
154    public Date getBirthdate() {
155        return birthdate;
156    }
157
158    @Override
159    public String getZoneInfo() {
160        return zoneInfo;
161    }
162
163    @Override
164    public String getLocale() {
165        return locale;
166    }
167
168    @Override
169    public String getPhoneNumber() {
170        return phoneNumber;
171    }
172
173    @Override
174    public String getAddress() {
175        return address;
176    }
177
178    @Override
179    public Date getUpdatedTime() {
180        Date date;
181        try {
182            DateTime dateTime = DateTime.parseRfc3339(updatedTime);
183            date = new Date(dateTime.getValue());
184        } catch (NumberFormatException e) {
185            return null;
186        }
187        return date;
188    }
189
190    @Key(value = "login")
191    protected String login;
192
193    @Override
194    public String getGivenName() {
195        return splitNameField(0);
196    }
197
198    @Override
199    public String getFamilyName() {
200        return splitNameField(1);
201    }
202
203    @Override
204    public String getNickname() {
205        return login;
206    }
207
208    public String getBlog() {
209        return blog;
210    }
211
212    public String getBio() {
213        return bio;
214    }
215
216    public String getCompany() {
217        return company;
218    }
219
220    public String getLogin() {
221        return login;
222    }
223
224    public String splitNameField(int index) {
225        String[] strings = name.split(" ");
226        if (index >= strings.length) {
227            return name;
228        } else {
229            return strings[index];
230        }
231    }
232}