use of org.apache.cxf.rs.security.oauth2.common.UserSubject in project cxf by apache.
the class TokenIntrospectionService method getTokenIntrospection.
@POST
@Produces({ MediaType.APPLICATION_JSON })
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
public TokenIntrospection getTokenIntrospection(@Encoded MultivaluedMap<String, String> params) {
checkSecurityContext();
String tokenId = params.getFirst(OAuthConstants.TOKEN_ID);
ServerAccessToken at = dataProvider.getAccessToken(tokenId);
if (at == null || OAuthUtils.isExpired(at.getIssuedAt(), at.getExpiresIn())) {
return new TokenIntrospection(false);
}
TokenIntrospection response = new TokenIntrospection(true);
response.setClientId(at.getClient().getClientId());
if (!at.getScopes().isEmpty()) {
response.setScope(OAuthUtils.convertPermissionsToScope(at.getScopes()));
}
UserSubject userSubject = at.getSubject();
if (userSubject != null) {
response.setUsername(at.getSubject().getLogin());
if (userSubject.getId() != null) {
response.setSub(userSubject.getId());
}
}
if (!StringUtils.isEmpty(at.getAudiences())) {
response.setAud(at.getAudiences());
}
if (at.getIssuer() != null) {
response.setIss(at.getIssuer());
}
response.setIat(at.getIssuedAt());
if (at.getExpiresIn() > 0) {
response.setExp(at.getIssuedAt() + at.getExpiresIn());
}
response.setTokenType(at.getTokenType());
if (reportExtraTokenProperties) {
response.getExtensions().putAll(at.getExtraProperties());
}
return response;
}
use of org.apache.cxf.rs.security.oauth2.common.UserSubject in project cxf by apache.
the class OAuthUtils method createSubject.
public static UserSubject createSubject(SecurityContext securityContext) {
List<String> roleNames = Collections.emptyList();
if (securityContext instanceof LoginSecurityContext) {
roleNames = new ArrayList<>();
Set<Principal> roles = ((LoginSecurityContext) securityContext).getUserRoles();
for (Principal p : roles) {
roleNames.add(p.getName());
}
}
UserSubject subject = new UserSubject(securityContext.getUserPrincipal().getName(), roleNames);
Message m = JAXRSUtils.getCurrentMessage();
if (m != null && m.get(AuthenticationMethod.class) != null) {
subject.setAuthenticationMethod(m.get(AuthenticationMethod.class));
}
return subject;
}
use of org.apache.cxf.rs.security.oauth2.common.UserSubject in project cxf by apache.
the class JPACodeDataProviderTest method addClient.
private Client addClient(String clientId, String userLogin) {
Client c = new Client();
c.setRedirectUris(Collections.singletonList("http://client/redirect"));
c.setClientId(clientId);
c.setResourceOwnerSubject(new UserSubject(userLogin));
getProvider().setClient(c);
return c;
}
use of org.apache.cxf.rs.security.oauth2.common.UserSubject in project cxf by apache.
the class JPAOAuthDataProviderTest method testAddGetDeleteClients.
@Test
public void testAddGetDeleteClients() {
Client c = addClient("12345", "alice");
Client c2 = addClient("56789", "alice");
Client c3 = addClient("09876", "bob");
List<Client> aliceClients = getProvider().getClients(new UserSubject("alice"));
assertNotNull(aliceClients);
assertEquals(2, aliceClients.size());
compareClients(c, aliceClients.get(0).getClientId().equals("12345") ? aliceClients.get(0) : aliceClients.get(1));
compareClients(c2, aliceClients.get(0).getClientId().equals("56789") ? aliceClients.get(0) : aliceClients.get(1));
List<Client> bobClients = getProvider().getClients(new UserSubject("bob"));
assertNotNull(bobClients);
assertEquals(1, bobClients.size());
Client bobClient = bobClients.get(0);
compareClients(c3, bobClient);
List<Client> allClients = getProvider().getClients(null);
assertNotNull(allClients);
assertEquals(3, allClients.size());
getProvider().removeClient(c.getClientId());
getProvider().removeClient(c2.getClientId());
getProvider().removeClient(c3.getClientId());
allClients = getProvider().getClients(null);
assertNotNull(allClients);
assertEquals(0, allClients.size());
}
use of org.apache.cxf.rs.security.oauth2.common.UserSubject in project cxf by apache.
the class JPAOAuthDataProviderTest method addClient.
protected Client addClient(String clientId, String userLogin) {
Client c = new Client();
c.setRedirectUris(Collections.singletonList("http://client/redirect"));
c.setClientId(clientId);
c.setClientSecret("123");
c.setResourceOwnerSubject(new UserSubject(userLogin));
getProvider().setClient(c);
return c;
}
Aggregations