private OauthToken toToken(OauthTokenDetail resp) { OauthToken token = new OauthToken(); token.owner = this; token.refreshToken = resp.refreshToken != null ? resp.refreshToken.token : null; token.accessToken = resp.accessToken.token; token.setAccount(resp.account); token.scope = join(resp.accessToken.scopes,","); token.tokenType = resp.accessToken.tokenType; token.uid = resp.uid; token.email = resp.email; token.userAccountRole = resp.userAccountRole; token.setExpiresIn(resp.accessToken.expiresIn); token.id = resp.id; return token; }
public CloudbeesPrincipalImpl(OauthToken token) { this.email = token.email; this.scopes = token.getScopes().toArray(new String[token.getScopes().size()]); this.accounts = token.listAccounts().toArray(new String[token.listAccounts().size()]); this.clientId = token.clientId; this.accessToken = token.accessToken; this.userAccountRole = token.userAccountRole; this.authenticationMethod = Secure.Authenticator.OAUTH; this.secured = true; }
public OauthToken validateToken(String token) throws OauthClientException { try{ OauthToken oauthToken = bees.jsonPOJORequest(gcUrl+"/oauth/tokens/"+token,null,OauthToken.class,"GET"); if(oauthToken.isExpired()){ return null; } return oauthToken; }catch (IOException e){ logger.log(Level.WARNING, "Failed to get token details",e); return null; } }
public @CheckForNull OauthToken get() { if (token==null) return null; OauthToken t = token.clone(); t.setExpiresIn(round(TimeUnit.MICROSECONDS.toSeconds(expiration - System.currentTimeMillis()))); return t; }
/** * Checks if this token has any of the scopes specified */ @CheckReturnValue public boolean validateScopes(String... scopes){ for (String s : scopes) { if (validateScope(s)) return true; } return false; }
public OauthToken validateToken(String token, String... scopes) throws OauthClientException { OauthToken oauthToken = validateToken(token); if (oauthToken==null) return null; if (oauthToken.validateScopes(scopes)) return oauthToken; else return null; }
@Override public OauthToken createOAuthClientToken(Collection<String> scopes) throws OauthClientException { OauthToken t = new OauthToken(); t.accessToken = "account="+account+",scope="+ join(scopes, " "); return t; } }
/** * Return true if the given scope is fond in the scopes granted with this token */ @JsonIgnore @CheckReturnValue public boolean validateScope(String scope){ if(scope == null){ return false; } for(String s: getScopes()){ if(s.trim().equals(scope)){ return true; } } return false; }
@Override public void authorizeRequest(HttpURLConnection con) throws IOException { try { createToken(r).asCredential().authorizeRequest(con); } catch (OauthClientException e) { throw (IOException)new IOException("Failed to authenticate the request").initCause(e); } } };
/** * Checks if this token has any of the scopes specified */ @CheckReturnValue public boolean validateScopes(String... scopes){ for (String s : scopes) { if (validateScope(s)) return true; } return false; }
public OauthToken validateToken(String token, String... scopes) throws OauthClientException { OauthToken oauthToken = validateToken(token); if (oauthToken==null) return null; if (oauthToken.validateScopes(scopes)) return oauthToken; else return null; }
@Override public OauthToken validateToken(String token) throws OauthClientException { // cue to return null? if (token.startsWith("invalid")) return null; OauthToken oa = new OauthToken(); for (String t : token.split(",")) { String[] lr = t.split("="); if (lr.length!=2) throw new IllegalArgumentException("Malformed mock token: "+t); Field f = FIELDS.get(lr[0]); if (f==null) throw new IllegalArgumentException("Invalid property: "+lr[0]+" expecting one of "+FIELDS.keySet()); try { f.set(oa,lr[1]); } catch (IllegalAccessException e) { throw new AssertionError(e); } } return oa; }
/** * Return true if the given scope is fond in the scopes granted with this token */ @JsonIgnore @CheckReturnValue public boolean validateScope(String scope){ if(scope == null){ return false; } for(String s: getScopes()){ if(s.trim().equals(scope)){ return true; } } return false; }
private OauthToken toToken(OauthTokenDetail resp) { OauthToken token = new OauthToken(); token.owner = this; token.refreshToken = resp.refreshToken != null ? resp.refreshToken.token : null; token.accessToken = resp.accessToken.token; token.setAccount(resp.account); token.scope = join(resp.accessToken.scopes,","); token.tokenType = resp.accessToken.tokenType; token.uid = resp.uid; token.email = resp.email; token.userAccountRole = resp.userAccountRole; token.setExpiresIn(resp.accessToken.expiresIn); token.id = resp.id; return token; }
/** * Checks if this token has a scope that matches the given domain name and capability. * * @param host * Represents the endpoint. */ @CheckReturnValue public boolean validateCapability(URL host, Capability cap) { return validateScope(cap.to(host)) || validateScope(cap.toAll()); }
public OauthToken validateToken(String token) throws OauthClientException { try{ OauthToken oauthToken = bees.jsonPOJORequest(gcUrl+"/oauth/tokens/"+token,null,OauthToken.class,"GET"); if(oauthToken.isExpired()){ return null; } return oauthToken; }catch (IOException e){ logger.log(Level.WARNING, "Failed to get token details",e); return null; } }
/** * Validates token with the given scopes. Returns null if the given access token is invalid, otherwise OauthToken is returned. * * <p> * {@link BeesClient} must be constructed with OAuth client ID and client secret as the username and password. * * @param token non-null token * @param scopes array of scope that are expected to be granted for this token * @return null if the token is invalid such as expired or unknown to the CloudBees OAuth server or the expected * scopes are not found. */ public final @CheckForNull OauthToken validateToken(String token, String... scopes) throws OauthClientException { OauthToken oauthToken = validateToken(token); if (oauthToken==null) return null; if (oauthToken.validateScopes(scopes)) return oauthToken; else return null; }
@Override public OauthToken createToken(TokenRequest r) throws OauthClientException { OauthToken t = new OauthToken(); t.accessToken = "account="+r.getAccountName()+",scope="+ join(r.getScopes(), " "); return t; }
/** * Checks if this token has a scope that matches the given domain name and capability. * * @param host * Represents the endpoint. */ @CheckReturnValue public boolean validateCapability(URL host, Capability cap) { return validateScope(cap.to(host)) || validateScope(cap.toAll()); }
/** * Gets the cached token valid in the cache, or if it's stale return null. */ private OauthToken getFromCache(String p) throws ExecutionException { CachedToken cache = this.cache.get(p); if (cache.isHalfExpired()) return null; OauthToken t = cache.get(); if(t==null || t.isExpired()){ return t; } return t; }