本文整理汇总了Java中com.google.gerrit.extensions.auth.oauth.OAuthToken类的典型用法代码示例。如果您正苦于以下问题:Java OAuthToken类的具体用法?Java OAuthToken怎么用?Java OAuthToken使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
OAuthToken类属于com.google.gerrit.extensions.auth.oauth包,在下文中一共展示了OAuthToken类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: retrieveJWTToken
import com.google.gerrit.extensions.auth.oauth.OAuthToken; //导入依赖的package包/类
private JsonObject retrieveJWTToken(OAuthToken token) {
JsonElement idToken = OutputFormat.JSON.newGson().fromJson(token.getRaw(), JsonElement.class);
if (idToken != null && idToken.isJsonObject()) {
JsonObject idTokenObj = idToken.getAsJsonObject();
JsonElement idTokenElement = idTokenObj.get("id_token");
if (idTokenElement != null && !idTokenElement.isJsonNull()) {
String payload = decodePayload(idTokenElement.getAsString());
if (!Strings.isNullOrEmpty(payload)) {
JsonElement tokenJsonElement =
OutputFormat.JSON.newGson().fromJson(payload, JsonElement.class);
if (tokenJsonElement.isJsonObject()) {
return tokenJsonElement.getAsJsonObject();
}
}
}
}
return null;
}
示例2: apply
import com.google.gerrit.extensions.auth.oauth.OAuthToken; //导入依赖的package包/类
@Override
public OAuthTokenInfo apply(AccountResource rsrc)
throws AuthException, ResourceNotFoundException {
if (self.get() != rsrc.getUser()) {
throw new AuthException("not allowed to get access token");
}
Account a = rsrc.getUser().getAccount();
OAuthToken accessToken = tokenCache.get(a.getId());
if (accessToken == null) {
throw new ResourceNotFoundException();
}
OAuthTokenInfo accessTokenInfo = new OAuthTokenInfo();
accessTokenInfo.username = a.getUserName();
accessTokenInfo.resourceHost = getHostName(canonicalWebUrlProvider.get());
accessTokenInfo.accessToken = accessToken.getToken();
accessTokenInfo.providerId = accessToken.getProviderId();
accessTokenInfo.expiresAt = Long.toString(accessToken.getExpiresAt());
accessTokenInfo.type = BEARER_TYPE;
return accessTokenInfo;
}
示例3: getUserInfo
import com.google.gerrit.extensions.auth.oauth.OAuthToken; //导入依赖的package包/类
@Override
public OAuthUserInfo getUserInfo(OAuthToken token) throws IOException {
OAuthRequest request = new OAuthRequest(Verb.GET, PROTECTED_RESOURCE_URL);
Token t = new Token(token.getToken(), token.getSecret(), token.getRaw());
service.signRequest(t, request);
Response response = request.send();
if (response.getCode() != SC_OK) {
throw new IOException(
String.format(
"Status %s (%s) for request %s",
response.getCode(), response.getBody(), request.getUrl()));
}
JsonElement userJson = JSON.newGson().fromJson(response.getBody(), JsonElement.class);
if (log.isDebugEnabled()) {
log.debug("User info response: {}", response.getBody());
}
if (userJson.isJsonObject()) {
JsonObject jsonObject = userJson.getAsJsonObject();
JsonObject userObject = jsonObject.getAsJsonObject("user");
if (userObject == null || userObject.isJsonNull()) {
throw new IOException("Response doesn't contain 'user' field");
}
JsonElement usernameElement = userObject.get("username");
String username = usernameElement.getAsString();
JsonElement displayName = jsonObject.get("display_name");
return new OAuthUserInfo(
BITBUCKET_PROVIDER_PREFIX + username,
username,
null,
displayName == null || displayName.isJsonNull() ? null : displayName.getAsString(),
fixLegacyUserId ? username : null);
}
throw new IOException(String.format("Invalid JSON '%s': not a JSON Object", userJson));
}
示例4: getUserInfo
import com.google.gerrit.extensions.auth.oauth.OAuthToken; //导入依赖的package包/类
@Override
public OAuthUserInfo getUserInfo(OAuthToken token) throws IOException {
final String protectedResourceUrl = String.format(PROTECTED_RESOURCE_URL, rootUrl);
OAuthRequest request = new OAuthRequest(Verb.GET, protectedResourceUrl);
Token t = new Token(token.getToken(), token.getSecret(), token.getRaw());
service.signRequest(t, request);
Response response = request.send();
if (response.getCode() != SC_OK) {
throw new IOException(
String.format(
"Status %s (%s) for request %s",
response.getCode(), response.getBody(), request.getUrl()));
}
JsonElement userJson = JSON.newGson().fromJson(response.getBody(), JsonElement.class);
if (log.isDebugEnabled()) {
log.debug("User info response: {}", response.getBody());
}
JsonObject jsonObject = userJson.getAsJsonObject();
if (jsonObject == null || jsonObject.isJsonNull()) {
throw new IOException("Response doesn't contain 'user' field" + jsonObject);
}
JsonElement id = jsonObject.get("id");
JsonElement username = jsonObject.get("username");
JsonElement email = jsonObject.get("email");
JsonElement name = jsonObject.get("name");
return new OAuthUserInfo(
GITLAB_PROVIDER_PREFIX + id.getAsString(),
username == null || username.isJsonNull() ? null : username.getAsString(),
email == null || email.isJsonNull() ? null : email.getAsString(),
name == null || name.isJsonNull() ? null : name.getAsString(),
null);
}
示例5: getAccessToken
import com.google.gerrit.extensions.auth.oauth.OAuthToken; //导入依赖的package包/类
@Override
public OAuthToken getAccessToken(OAuthVerifier rv) {
Verifier vi = new Verifier(rv.getValue());
Token to = service.getAccessToken(null, vi);
OAuthToken result = new OAuthToken(to.getToken(), to.getSecret(), to.getRawResponse());
return result;
}
示例6: getUserInfo
import com.google.gerrit.extensions.auth.oauth.OAuthToken; //导入依赖的package包/类
@Override
public OAuthUserInfo getUserInfo(OAuthToken token) throws IOException {
JsonElement tokenJson = JSON.newGson().fromJson(token.getRaw(), JsonElement.class);
JsonObject tokenObject = tokenJson.getAsJsonObject();
JsonElement id_token = tokenObject.get("id_token");
JsonElement claimJson =
JSON.newGson().fromJson(parseJwt(id_token.getAsString()), JsonElement.class);
// Dex does not support basic profile currently (2017-09), extracting info
// from access token claim
JsonObject claimObject = claimJson.getAsJsonObject();
JsonElement emailElement = claimObject.get("email");
JsonElement nameElement = claimObject.get("name");
if (emailElement == null || emailElement.isJsonNull()) {
throw new IOException(String.format("Response doesn't contain email field"));
}
if (nameElement == null || nameElement.isJsonNull()) {
throw new IOException(String.format("Response doesn't contain name field"));
}
String email = emailElement.getAsString();
String name = nameElement.getAsString();
String username = email;
if (domain != null && domain.length() > 0) {
username = email.replace("@" + domain, "");
}
return new OAuthUserInfo(
DEX_PROVIDER_PREFIX + email /*externalId*/,
username /*username*/,
email /*email*/,
name /*displayName*/,
null /*claimedIdentity*/);
}
示例7: getAccessToken
import com.google.gerrit.extensions.auth.oauth.OAuthToken; //导入依赖的package包/类
@Override
public OAuthToken getAccessToken(OAuthVerifier rv) {
Verifier vi = new Verifier(rv.getValue());
Token to = service.getAccessToken(null, vi);
OAuthToken result = new OAuthToken(to.getToken(), to.getSecret(), to.getRawResponse());
return result;
}
示例8: getUserInfo
import com.google.gerrit.extensions.auth.oauth.OAuthToken; //导入依赖的package包/类
@Override
public OAuthUserInfo getUserInfo(OAuthToken token) throws IOException {
OAuthRequest request = new OAuthRequest(Verb.GET, PROTECTED_RESOURCE_URL);
Token t = new Token(token.getToken(), token.getSecret(), token.getRaw());
service.signRequest(t, request);
Response response = request.send();
if (response.getCode() != HttpServletResponse.SC_OK) {
throw new IOException(
String.format(
"Status %s (%s) for request %s",
response.getCode(), response.getBody(), request.getUrl()));
}
JsonElement userJson =
OutputFormat.JSON.newGson().fromJson(response.getBody(), JsonElement.class);
if (log.isDebugEnabled()) {
log.debug("User info response: {}", response.getBody());
}
if (userJson.isJsonObject()) {
JsonObject jsonObject = userJson.getAsJsonObject();
JsonElement id = jsonObject.get("id");
if (id == null || id.isJsonNull()) {
throw new IOException(String.format("Response doesn't contain id field"));
}
JsonElement email = jsonObject.get("email");
JsonElement name = jsonObject.get("name");
JsonElement login = jsonObject.get("login");
return new OAuthUserInfo(
GITHUB_PROVIDER_PREFIX + id.getAsString(),
login == null || login.isJsonNull() ? null : login.getAsString(),
email == null || email.isJsonNull() ? null : email.getAsString(),
name == null || name.isJsonNull() ? null : name.getAsString(),
fixLegacyUserId ? id.getAsString() : null);
}
throw new IOException(String.format("Invalid JSON '%s': not a JSON Object", userJson));
}
示例9: getUserInfo
import com.google.gerrit.extensions.auth.oauth.OAuthToken; //导入依赖的package包/类
@Override
public OAuthUserInfo getUserInfo(OAuthToken token) throws IOException {
JsonElement tokenJson = JSON.newGson().fromJson(token.getRaw(), JsonElement.class);
JsonObject tokenObject = tokenJson.getAsJsonObject();
JsonElement id_token = tokenObject.get("id_token");
JsonElement claimJson =
JSON.newGson().fromJson(parseJwt(id_token.getAsString()), JsonElement.class);
JsonObject claimObject = claimJson.getAsJsonObject();
if (log.isDebugEnabled()) {
log.debug("Claim object: {}", claimObject);
}
JsonElement usernameElement = claimObject.get("preferred_username");
JsonElement emailElement = claimObject.get("email");
JsonElement nameElement = claimObject.get("name");
if (usernameElement == null || usernameElement.isJsonNull()) {
throw new IOException("Response doesn't contain preferred_username field");
}
if (emailElement == null || emailElement.isJsonNull()) {
throw new IOException("Response doesn't contain email field");
}
if (nameElement == null || nameElement.isJsonNull()) {
throw new IOException("Response doesn't contain name field");
}
String username = usernameElement.getAsString();
String email = emailElement.getAsString();
String name = nameElement.getAsString();
return new OAuthUserInfo(
KEYCLOAK_PROVIDER_PREFIX + username /*externalId*/,
username /*username*/,
email /*email*/,
name /*displayName*/,
null /*claimedIdentity*/);
}
示例10: login
import com.google.gerrit.extensions.auth.oauth.OAuthToken; //导入依赖的package包/类
boolean login(
HttpServletRequest request, HttpServletResponse response, OAuthServiceProvider oauth)
throws IOException {
log.debug("Login " + this);
if (isOAuthFinal(request)) {
if (!checkState(request)) {
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return false;
}
log.debug("Login-Retrieve-User " + this);
OAuthToken token = oauth.getAccessToken(new OAuthVerifier(request.getParameter("code")));
user = oauth.getUserInfo(token);
if (isLoggedIn()) {
log.debug("Login-SUCCESS " + this);
authenticateAndRedirect(request, response, token);
return true;
}
response.sendError(SC_UNAUTHORIZED);
return false;
}
log.debug("Login-PHASE1 " + this);
redirectToken = request.getRequestURI();
// We are here in content of filter.
// Due to this Jetty limitation:
// https://bz.apache.org/bugzilla/show_bug.cgi?id=28323
// we cannot use LoginUrlToken.getToken() method,
// because it relies on getPathInfo() and it is always null here.
redirectToken = redirectToken.substring(request.getContextPath().length());
response.sendRedirect(oauth.getAuthorizationUrl() + "&state=" + state);
return false;
}
示例11: authenticateAndRedirect
import com.google.gerrit.extensions.auth.oauth.OAuthToken; //导入依赖的package包/类
private void authenticateAndRedirect(
HttpServletRequest req, HttpServletResponse rsp, OAuthToken token) throws IOException {
AuthRequest areq = new AuthRequest(ExternalId.Key.parse(user.getExternalId()));
AuthResult arsp;
try {
String claimedIdentifier = user.getClaimedIdentity();
if (!Strings.isNullOrEmpty(claimedIdentifier)) {
if (!authenticateWithIdentityClaimedDuringHandshake(areq, rsp, claimedIdentifier)) {
return;
}
} else if (linkMode) {
if (!authenticateWithLinkedIdentity(areq, rsp)) {
return;
}
}
areq.setUserName(user.getUserName());
areq.setEmailAddress(user.getEmailAddress());
areq.setDisplayName(user.getDisplayName());
arsp = accountManager.authenticate(areq);
accountId = arsp.getAccountId();
tokenCache.put(accountId, token);
} catch (AccountException e) {
log.error("Unable to authenticate user \"" + user + "\"", e);
rsp.sendError(HttpServletResponse.SC_FORBIDDEN);
return;
}
webSession.get().login(arsp, true);
String suffix = redirectToken.substring(OAuthWebFilter.GERRIT_LOGIN.length() + 1);
suffix = CharMatcher.anyOf("/").trimLeadingFrom(Url.decode(suffix));
StringBuilder rdr = new StringBuilder(urlProvider.get(req));
rdr.append(suffix);
rsp.sendRedirect(rdr.toString());
}
示例12: module
import com.google.gerrit.extensions.auth.oauth.OAuthToken; //导入依赖的package包/类
public static Module module() {
return new CacheModule() {
@Override
protected void configure() {
persist(OAUTH_TOKENS, Account.Id.class, OAuthToken.class);
}
};
}
示例13: OAuthTokenCache
import com.google.gerrit.extensions.auth.oauth.OAuthToken; //导入依赖的package包/类
@Inject
OAuthTokenCache(
@Named(OAUTH_TOKENS) Cache<Account.Id, OAuthToken> cache,
DynamicItem<OAuthTokenEncrypter> encrypter) {
this.cache = cache;
this.encrypter = encrypter;
}
示例14: get
import com.google.gerrit.extensions.auth.oauth.OAuthToken; //导入依赖的package包/类
public OAuthToken get(Account.Id id) {
OAuthToken accessToken = cache.getIfPresent(id);
if (accessToken == null) {
return null;
}
accessToken = decrypt(accessToken);
if (accessToken.isExpired()) {
cache.invalidate(id);
return null;
}
return accessToken;
}
示例15: encrypt
import com.google.gerrit.extensions.auth.oauth.OAuthToken; //导入依赖的package包/类
private OAuthToken encrypt(OAuthToken token) {
OAuthTokenEncrypter enc = encrypter.get();
if (enc == null) {
return token;
}
return enc.encrypt(token);
}