本文整理汇总了Java中org.osiam.client.oauth.AccessToken类的典型用法代码示例。如果您正苦于以下问题:Java AccessToken类的具体用法?Java AccessToken怎么用?Java AccessToken使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
AccessToken类属于org.osiam.client.oauth包,在下文中一共展示了AccessToken类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: validateToken
import org.osiam.client.oauth.AccessToken; //导入依赖的package包/类
@RequestMapping(value = "/validation", method = RequestMethod.POST)
@ResponseBody
public AccessToken validateToken(@RequestHeader("Authorization") final String authorization) {
String token = getToken(authorization);
OAuth2Authentication auth = tokenStore.readAuthentication(token);
OAuth2AccessToken accessToken = tokenStore.getAccessToken(auth);
OAuth2Request authReq = auth.getOAuth2Request();
AccessToken.Builder tokenBuilder = new AccessToken.Builder(token).setClientId(authReq.getClientId());
if (auth.getUserAuthentication() != null && auth.getPrincipal() instanceof User) {
User user = (User) auth.getPrincipal();
tokenBuilder.setUserName(user.getUserName());
tokenBuilder.setUserId(user.getId());
}
tokenBuilder.setExpiresAt(accessToken.getExpiration());
for (String scopeString : authReq.getScope()) {
tokenBuilder.addScope(new Scope(scopeString));
}
return tokenBuilder.build();
}
示例2: createAccessToken
import org.osiam.client.oauth.AccessToken; //导入依赖的package包/类
public AccessToken createAccessToken() {
Set<String> scopes = new HashSet<>();
scopes.add(Scope.ADMIN.toString());
// Random scope, because the token services generates for every scope but same client
// a different access token. This is only made due to the token expired problem, when the auth server
// takes his actual access token, but the token is expired during the request to the resource server
scopes.add(new Scope(UUID.randomUUID().toString()).toString());
Map<String, String> parameters = new HashMap<>();
parameters.put("client_id", OsiamAuthServerClientProvider.AUTH_SERVER_CLIENT_ID);
OAuth2Request authRequest = new OAuth2Request(
parameters, OsiamAuthServerClientProvider.AUTH_SERVER_CLIENT_ID, null, true, scopes,
null, null, null, null
);
OAuth2Authentication oAuth2Authentication = new OAuth2Authentication(authRequest, null);
OAuth2AccessToken oAuth2AccessToken = new DefaultOAuth2AccessToken(UUID.randomUUID().toString());
tokenStore.storeAccessToken(oAuth2AccessToken, oAuth2Authentication);
return new AccessToken.Builder(oAuth2AccessToken.getValue()).build();
}
示例3: validateToken
import org.osiam.client.oauth.AccessToken; //导入依赖的package包/类
public AccessToken validateToken(final String token) {
OAuth2Authentication auth = tokenStore.readAuthentication(token);
OAuth2AccessToken accessToken = tokenStore.getAccessToken(auth);
OAuth2Request authReq = auth.getOAuth2Request();
AccessToken.Builder tokenBuilder = new AccessToken.Builder(token).setClientId(authReq.getClientId());
if (auth.getUserAuthentication() != null && auth.getPrincipal() instanceof User) {
User user = (User) auth.getPrincipal();
tokenBuilder.setUserName(user.getUserName());
tokenBuilder.setUserId(user.getId());
}
tokenBuilder.setExpiresAt(accessToken.getExpiration());
for (String scopeString : authReq.getScope()) {
tokenBuilder.addScope(new Scope(scopeString));
}
return tokenBuilder.build();
}
示例4: loadAuthentication
import org.osiam.client.oauth.AccessToken; //导入依赖的package包/类
@Override
public OAuth2Authentication loadAuthentication(String token) {
AccessToken accessToken = validateAccessToken(token);
Set<String> scopes = new HashSet<>();
if (accessToken.getScopes() != null) {
for (Scope scope : accessToken.getScopes()) {
scopes.add(scope.toString());
}
}
Map<String, String> parameters = new HashMap<>();
parameters.put(AccessTokenConverter.CLIENT_ID, accessToken.getClientId());
OAuth2Request authRequest = new OAuth2Request(parameters, accessToken.getClientId(), null, true, scopes, null, null,
null, null);
Authentication auth = null;
if (!accessToken.isClientOnly()) {
User authUser = new User.Builder(accessToken.getUserName()).setId(accessToken.getUserId()).build();
auth = new UsernamePasswordAuthenticationToken(authUser, null, new ArrayList<GrantedAuthority>());
}
return new OAuth2Authentication(authRequest, auth);
}
示例5: readAccessToken
import org.osiam.client.oauth.AccessToken; //导入依赖的package包/类
@Override
public OAuth2AccessToken readAccessToken(String token) {
AccessToken accessToken = validateAccessToken(token);
Set<String> scopes = new HashSet<>();
for (Scope scope : accessToken.getScopes()) {
scopes.add(scope.toString());
}
DefaultOAuth2AccessToken oAuth2AccessToken = new DefaultOAuth2AccessToken(token);
oAuth2AccessToken.setScope(scopes);
oAuth2AccessToken.setExpiration(accessToken.getExpiresAt());
oAuth2AccessToken.setTokenType(DefaultOAuth2AccessToken.BEARER_TYPE);
return oAuth2AccessToken;
}
示例6: convertOutput
import org.osiam.client.oauth.AccessToken; //导入依赖的package包/类
@Override
public Object convertOutput(Object toBeFormatted) {
if(toBeFormatted instanceof AccessToken){
final AccessToken at = ((AccessToken)toBeFormatted);
final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
StringBuilder sb = new StringBuilder();
sb.append("Token: ");
sb.append(at.getToken());
sb.append("\nType: ");
sb.append(at.getType());
sb.append("\nExpires: ");
sb.append(sdf.format(at.getExpiresAt()));
return sb;
}
return null;
}
示例7: testRemoveUserFromGroup
import org.osiam.client.oauth.AccessToken; //导入依赖的package包/类
@Test
public void testRemoveUserFromGroup() {
List<Group> groups = new ArrayList<Group>();
groups.add(new Group.Builder("group").build());
SCIMSearchResult<Group> result = mock(SCIMSearchResult.class);
when(result.getResources()).thenReturn(groups);
when(con.searchGroups(eq(OsiamContext.createGroupFilter("group")), any(AccessToken.class))).thenReturn(result);
assertFalse(oc.removeUserFromGroup("user", "group"));
List<GroupRef> groupRefs = new ArrayList<GroupRef>();
groupRefs.add(new GroupRef.Builder().setDisplay("group").build());
User groupUser = new User.Builder("user").setId("userid").setGroups(groupRefs).build();
List<User> users = new ArrayList<User>();
users.add(groupUser);
SCIMSearchResult<User> result2 = mock(SCIMSearchResult.class);
when(result2.getResources()).thenReturn(users);
when(con.searchUsers(eq(OsiamContext.createUserFilter("user")), any(AccessToken.class))).thenReturn(result2);
assertTrue(oc.removeUserFromGroup("user", "group"));
}
示例8: testCreateGroup
import org.osiam.client.oauth.AccessToken; //导入依赖的package包/类
@Test
public void testCreateGroup() {
List<Group> groups = new ArrayList<Group>();
SCIMSearchResult<Group> result = mock(SCIMSearchResult.class);
when(result.getResources()).thenReturn(groups);
when(con.searchGroups(eq(OsiamContext.createGroupFilter("group")), any(AccessToken.class))).thenReturn(result);
Group group = new Group.Builder("group").build();
when(con.createGroup(eq(group), any(AccessToken.class))).thenReturn(group);
// if group does not exist, we can create it and new group is returned
assertEquals(group, oc.createGroup(group));
groups.add(group);
// if group already exists, null is returned.
assertNull(oc.createGroup(group));
}
示例9: testGetUsersInGroup
import org.osiam.client.oauth.AccessToken; //导入依赖的package包/类
@Test
public void testGetUsersInGroup() {
List<User> users = new ArrayList<User>();
List<GroupRef> groupA = new ArrayList<GroupRef>();
groupA.add(new GroupRef.Builder().setDisplay("groupA").build());
List<GroupRef> groupB = new ArrayList<GroupRef>();
groupB.add(new GroupRef.Builder().setDisplay("groupB").build());
users.add(new User.Builder("user1").setGroups(groupA).build());
users.add(new User.Builder("user2").setGroups(groupB).build());
when(con.getAllUsers(any(AccessToken.class))).thenReturn(new ArrayList(users));
List<User> result = oc.getUsersWithGroup("groupA", false);
assertEquals(1, result.size());
assertEquals("user1", result.get(0).getUserName());
when(con.getAllUsers(any(AccessToken.class))).thenReturn(new ArrayList(users));
result = oc.getUsersWithGroup("groupA", true);
assertEquals(1, result.size());
assertEquals("user2", result.get(0).getUserName());
}
示例10: testOAuth
import org.osiam.client.oauth.AccessToken; //导入依赖的package包/类
@Test
public void testOAuth() {
OsiamConnector.Builder bldr = mock(OsiamConnector.Builder.class);
when(bldr.setGrantType(any(GrantType.class))).thenReturn(bldr);
when(bldr.setScope(any(Scope.class))).thenReturn(bldr);
when(bldr.setClientRedirectUri(anyString())).thenReturn(bldr);
OsiamConnector con = mock(OsiamConnector.class);
when(bldr.build()).thenReturn(con);
AccessToken at = new SimpleAccessToken("accesstoken");
when(con.retrieveAccessToken(eq("authcode"))).thenReturn(at);
when(od.getBuilder()).thenReturn(bldr);
Response r = or.oauth2("authcode", "state");
NewCookie c = (NewCookie)r.getMetadata().getFirst("Set-Cookie");
assertEquals(c.getValue(), "accesstoken");
URI u = (URI)r.getMetadata().getFirst("Location");
assertEquals(u.toString(), "http://state");
}
示例11: getCurrentUserBasic
import org.osiam.client.oauth.AccessToken; //导入依赖的package包/类
/**
* See {@link OsiamConnector#getCurrentUserBasic(AccessToken)}
*
* @deprecated The BasicUser class has been deprecated. Use {@link #getMe(AccessToken)} with OSIAM 3.x. This method
* is going to go away with version 1.12 or 2.0.
*/
@Deprecated
BasicUser getCurrentUserBasic(AccessToken accessToken) {
checkAccessTokenIsNotNull(accessToken);
StatusType status;
String content;
try {
Response response = targetEndpoint.path("me").request(MediaType.APPLICATION_JSON)
.header("Authorization", BEARER + accessToken.getToken())
.property(ClientProperties.CONNECT_TIMEOUT, getConnectTimeout())
.property(ClientProperties.READ_TIMEOUT, getReadTimeout())
.get();
status = response.getStatusInfo();
content = response.readEntity(String.class);
} catch (ProcessingException e) {
throw new ConnectionInitializationException(CONNECTION_SETUP_ERROR_STRING, e);
}
checkAndHandleResponse(content, status, accessToken);
return mapToType(content, BasicUser.class);
}
示例12: can_replace_other_user
import org.osiam.client.oauth.AccessToken; //导入依赖的package包/类
@Test
public void can_replace_other_user() {
AccessToken accessToken = OSIAM_CONNECTOR.retrieveAccessToken("marissa", "koala", Scope.ADMIN);
User originalUser = OSIAM_CONNECTOR.getUser(OTHER_USER_ID, accessToken);
Email email = new Email.Builder()
.setValue("[email protected]")
.setType(Email.Type.HOME)
.build();
User replaceUser = new User.Builder(originalUser)
.setDisplayName("Barbara")
.setActive(false)
.addEmail(email)
.build();
User user = OSIAM_CONNECTOR.replaceUser(OTHER_USER_ID, replaceUser, accessToken);
assertThat(user.getDisplayName(), is(equalTo("Barbara")));
assertThat(user.isActive(), is(equalTo(false)));
assertThat(user.getEmails().get(0).getValue(), is(equalTo("[email protected]")));
assertThat(user.getEmails().get(0).getType(), is(equalTo(Email.Type.HOME)));
}
示例13: deleteResource
import org.osiam.client.oauth.AccessToken; //导入依赖的package包/类
void deleteResource(String id, AccessToken accessToken) {
checkArgument(!Strings.isNullOrEmpty(id), "The given id must not be null nor empty.");
checkAccessTokenIsNotNull(accessToken);
StatusType status;
String content;
try {
Response response = targetEndpoint.path(typeName + "s").path(id).request(MediaType.APPLICATION_JSON)
.header(AUTHORIZATION, BEARER + accessToken.getToken())
.property(ClientProperties.CONNECT_TIMEOUT, connectTimeout)
.property(ClientProperties.READ_TIMEOUT, readTimeout)
.delete();
status = response.getStatusInfo();
content = response.readEntity(String.class);
} catch (ProcessingException e) {
throw new ConnectionInitializationException(CONNECTION_SETUP_ERROR_STRING, e);
}
checkAndHandleResponse(content, status, accessToken);
}
示例14: can_replace_own_user
import org.osiam.client.oauth.AccessToken; //导入依赖的package包/类
@Test
public void can_replace_own_user() {
AccessToken accessToken = OSIAM_CONNECTOR.retrieveAccessToken("marissa", "koala", Scope.ADMIN);
User originalUser = OSIAM_CONNECTOR.getUser(OWN_USER_ID, accessToken);
Email email = new Email.Builder()
.setValue("[email protected]")
.setType(Email.Type.HOME)
.build();
User replaceUser = new User.Builder(originalUser)
.setDisplayName("Marissa")
.setActive(false)
.addEmail(email)
.build();
User user = OSIAM_CONNECTOR.replaceUser(OWN_USER_ID, replaceUser, accessToken);
assertThat(user.getDisplayName(), is(equalTo("Marissa")));
assertThat(user.isActive(), is(equalTo(false)));
assertThat(user.getEmails().get(0).getValue(), is(equalTo("[email protected]")));
assertThat(user.getEmails().get(0).getType(), is(equalTo(Email.Type.HOME)));
}
示例15: validateAccessToken
import org.osiam.client.oauth.AccessToken; //导入依赖的package包/类
/**
* @see OsiamConnector#validateAccessToken(AccessToken)
*/
AccessToken validateAccessToken(AccessToken tokenToValidate) {
checkNotNull(tokenToValidate, "The tokenToValidate must not be null.");
StatusType status;
String content;
try {
Response response = targetEndpoint.path(TOKEN_VALIDATION_ENDPOINT)
.request(MediaType.APPLICATION_JSON)
.property(HttpAuthenticationFeature.HTTP_AUTHENTICATION_USERNAME, clientId)
.property(HttpAuthenticationFeature.HTTP_AUTHENTICATION_PASSWORD, clientSecret)
.property(ClientProperties.CONNECT_TIMEOUT, connectTimeout)
.property(ClientProperties.READ_TIMEOUT, readTimeout)
.header(AUTHORIZATION_HEADER, BEARER + tokenToValidate.getToken())
.post(Entity.json(""));
status = response.getStatusInfo();
content = response.readEntity(String.class);
} catch (ProcessingException e) {
throw createGeneralConnectionInitializationException(e);
}
checkAndHandleResponse(content, status, tokenToValidate);
return getAccessToken(content);
}