static TokenInfo fromString(String val) { String[] parts = val.split("-----"); TokenInfo info = new TokenInfo(); info.accessToken = parts[0]; info.expires = parts.length > 1 ? parts[1] : null; return info; }
// This method is called via a global method defined in AuthImpl.register() @SuppressWarnings("unused") void finish(String hash) { TokenInfo info = new TokenInfo(); String error = null; String errorDesc = ""; String errorUri = ""; // Iterate over keys and values in the string hash value to find relevant // information like the access token or an error message. The string will be // in the form of: #key1=val1&key2=val2&key3=val3 (etc.) int idx = 1; while (idx < hash.length() - 1) { // Grab the next key (between start and '=') int nextEq = hash.indexOf('=', idx); if (nextEq < 0) { break; } String key = hash.substring(idx, nextEq); // Grab the next value (between '=' and '&') int nextAmp = hash.indexOf('&', nextEq); nextAmp = nextAmp < 0 ? hash.length() : nextAmp; String val = hash.substring(nextEq + 1, nextAmp); // Start looking from here from now on. idx = nextAmp + 1; // Store relevant values to be used later. if (key.equals("access_token")) { info.accessToken = val; } else if (key.equals("expires_in")) { // expires_in is seconds, convert to milliseconds and add to now Double expiresIn = Double.valueOf(val) * 1000; info.expires = String.valueOf(clock.now() + expiresIn); } else if (key.equals("error")) { error = val; } else if (key.equals("error_description")) { errorDesc = " (" + val + ")"; } else if (key.equals("error_uri")) { errorUri = "; see: " + val; } } if (error != null) { lastCallback.onFailure( new RuntimeException("Error from provider: " + error + errorDesc + errorUri)); } else if (info.accessToken == null) { lastCallback.onFailure(new RuntimeException("Could not find access_token in hash " + hash)); } else { setToken(lastRequest, info); lastCallback.onSuccess(info.accessToken); } }
/** * Show certificate. * * @param certId certificate id * @throws Exception if an error occurs */ @Command(description = "Show certificate") public void showCertificate(@Param(name = "certId", description = "Certificate ID") String certId) throws Exception { List<TokenInfo> tokens = SignerClient.execute(new ListTokens()); for (TokenInfo token : tokens) { for (KeyInfo key : token.getKeyInfo()) { for (CertificateInfo cert : key.getCerts()) { if (certId.equals(cert.getId())) { X509Certificate x509 = readCertificate(cert.getCertificateBytes()); System.out.println(x509); return; } } } } System.out.println("Certificate " + certId + " not found"); }
public void setTokenInfo( SecurityToken securityToken, ConsumerInfo consumerInfo, String serviceName, String tokenName, TokenInfo tokenInfo) throws GadgetException { long userId = GetterUtil.getLong(securityToken.getViewerId()); User user = null; try { user = UserLocalServiceUtil.getUser(userId); } catch (Exception e) { throw new GadgetException(GadgetException.Code.INTERNAL_SERVER_ERROR, e); } Gadget gadget = null; try { gadget = GadgetLocalServiceUtil.getGadget(user.getCompanyId(), securityToken.getAppUrl()); } catch (Exception e) { throw new GadgetException(GadgetException.Code.INTERNAL_SERVER_ERROR, e); } try { OAuthTokenLocalServiceUtil.addOAuthToken( userId, gadget.getGadgetId(), serviceName, securityToken.getModuleId(), tokenInfo.getAccessToken(), tokenName, tokenInfo.getTokenSecret(), tokenInfo.getSessionHandle(), tokenInfo.getTokenExpireMillis()); } catch (Exception e) { throw new GadgetException(GadgetException.Code.INTERNAL_SERVER_ERROR, e); } }
/* * @param req The authentication request of which to request the expiration * status. * @return The number of milliseconds until the token expires, or negative * infinity if no token was found. */ public double expiresIn(AuthRequest req) { String val = tokenStore.get(req.asString()); return val == null ? Double.NEGATIVE_INFINITY : Double.valueOf(TokenInfo.fromString(val).expires) - clock.now(); }
void setToken(AuthRequest req, TokenInfo info) { tokenStore.set(req.asString(), info.asString()); }
TokenInfo getToken(AuthRequest req) { String tokenStr = tokenStore.get(req.asString()); return tokenStr != null ? TokenInfo.fromString(tokenStr) : null; }