-
Notifications
You must be signed in to change notification settings - Fork 824
/
UserInfoEndpoint.java
88 lines (76 loc) · 3.64 KB
/
UserInfoEndpoint.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*******************************************************************************
* Cloud Foundry
* Copyright (c) [2009-2016] Pivotal Software, Inc. All Rights Reserved.
*
* This product is licensed to you under the Apache License, Version 2.0 (the "License").
* You may not use this product except in compliance with the License.
*
* This product includes a number of subcomponents with
* separate copyright notices and license terms. Your use of these
* subcomponents is subject to the terms and conditions of the
* subcomponent's license, as noted in the LICENSE file.
*******************************************************************************/
package org.cloudfoundry.identity.uaa.account;
import org.cloudfoundry.identity.uaa.authentication.UaaPrincipal;
import org.cloudfoundry.identity.uaa.user.UaaUser;
import org.cloudfoundry.identity.uaa.user.UaaUserDatabase;
import org.cloudfoundry.identity.uaa.user.UserInfo;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.expression.OAuth2ExpressionUtils;
import org.springframework.stereotype.Controller;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.security.Principal;
import static org.cloudfoundry.identity.uaa.oauth.token.ClaimConstants.USER_ATTRIBUTES;
/**
* Controller that sends user info to clients wishing to authenticate.
*
* @author Dave Syer
*/
@Controller
public class UserInfoEndpoint implements InitializingBean {
private UaaUserDatabase userDatabase;
public void setUserDatabase(UaaUserDatabase userDatabase) {
this.userDatabase = userDatabase;
}
@Override
public void afterPropertiesSet() throws Exception {
Assert.state(userDatabase != null, "A user database must be provided");
}
@RequestMapping(value = "/userinfo")
@ResponseBody
public UserInfoResponse loginInfo(Principal principal) {
OAuth2Authentication authentication = (OAuth2Authentication) principal;
UaaPrincipal uaaPrincipal = extractUaaPrincipal(authentication);
boolean addCustomAttributes = OAuth2ExpressionUtils.hasAnyScope(authentication, new String[] {USER_ATTRIBUTES});
return getResponse(uaaPrincipal, addCustomAttributes);
}
protected UaaPrincipal extractUaaPrincipal(OAuth2Authentication authentication) {
Object object = authentication.getUserAuthentication().getPrincipal();
if (object instanceof UaaPrincipal) {
return (UaaPrincipal) object;
}
throw new IllegalStateException("User authentication could not be converted to UaaPrincipal");
}
protected UserInfoResponse getResponse(UaaPrincipal principal, boolean addCustomAttributes) {
UaaUser user = userDatabase.retrieveUserById(principal.getId());
UserInfoResponse response = new UserInfoResponse();
response.setUserId(user.getId());
response.setUsername(user.getUsername());
response.setGivenName(user.getGivenName());
response.setFamilyName(user.getFamilyName());
response.setEmail(user.getEmail());
response.setPhoneNumber(user.getPhoneNumber());
response.setSub(user.getId());
// TODO: other attributes
if (addCustomAttributes) {
UserInfo info = userDatabase.getUserInfo(user.getId());
if (info!=null && info.size()>0) {
response.setAttributeValue(USER_ATTRIBUTES, info);
}
}
return response;
}
}