Exemplo n.º 1
0
 private OAuthAccessToken parseUrlEncodedToken(HttpMessage response) {
   Map<String, String[]> params = new HashMap<String, String[]>();
   AuthUtils.parseFormUrlEncoded(response, params);
   if (response.getStatus() == 200) {
     String accessTokenE = AuthUtils.getParamValue(params, "access_token");
     if (accessTokenE != null) {
       String accessToken = accessTokenE;
       WDate expires = null;
       String expiresE = AuthUtils.getParamValue(params, "expires");
       if (expiresE != null) {
         expires = new WDate(new Date()).addSeconds(Integer.parseInt(expiresE));
       }
       return new OAuthAccessToken(accessToken, expires, "");
     } else {
       throw new OAuthProcess.TokenError(WString.tr("Wt.Auth.OAuthService.badresponse"));
     }
   } else {
     String errorE = AuthUtils.getParamValue(params, "error");
     if (errorE != null) {
       throw new OAuthProcess.TokenError(WString.tr("Wt.Auth.OAuthService." + errorE));
     } else {
       throw new OAuthProcess.TokenError(WString.tr("Wt.Auth.OAuthService.badresponse"));
     }
   }
 }
Exemplo n.º 2
0
 /**
  * Parses the response for a token request.
  *
  * <p>Throws a {@link TokenError} when the response indicates an error, or when the response could
  * not be properly parsed.
  *
  * <p>Some OAuth implementations may uses a non-standard encoding of the token.
  */
 protected OAuthAccessToken parseTokenResponse(HttpMessage response) {
   if (response.getStatus() == 200 || response.getStatus() == 400) {
     String type = response.getHeader("Content-Type");
     if (type != null) {
       if (type.startsWith("text/plain; charset=UTF-8")) {
         return this.parseUrlEncodedToken(response);
       } else {
         if (type.startsWith("application/json")) {
           return this.parseJsonToken(response);
         } else {
           throw new OAuthProcess.TokenError(WString.tr("Wt.Auth.OAuthService.badresponse"));
         }
       }
     } else {
       throw new OAuthProcess.TokenError(WString.tr("Wt.Auth.OAuthService.badresponse"));
     }
   } else {
     throw new OAuthProcess.TokenError(WString.tr("Wt.Auth.OAuthService.badresponse"));
   }
 }
Exemplo n.º 3
0
 void requestToken(String authorizationCode) {
   try {
     String url = this.service_.getTokenEndpoint();
     StringBuilder ss = new StringBuilder();
     ss.append("grant_type=authorization_code")
         .append("&client_id=")
         .append(Utils.urlEncode(this.service_.getClientId()))
         .append("&client_secret=")
         .append(Utils.urlEncode(this.service_.getClientSecret()))
         .append("&redirect_uri=")
         .append(Utils.urlEncode(this.service_.getGenerateRedirectEndpoint()))
         .append("&code=")
         .append(authorizationCode);
     HttpClient client = new HttpClient(this);
     client.setTimeout(15);
     client
         .done()
         .addListener(
             this,
             new Signal2.Listener<Exception, HttpMessage>() {
               public void trigger(Exception event1, HttpMessage event2) {
                 OAuthProcess.this.handleToken(event1, event2);
               }
             });
     Method m = this.service_.getTokenRequestMethod();
     if (m == Method.Get) {
       boolean hasQuery = url.indexOf('?') != -1;
       url += (hasQuery ? '&' : '?') + ss.toString();
       client.get(url);
     } else {
       HttpMessage post = new HttpMessage();
       post.setHeader("Content-Type", "application/x-www-form-urlencoded");
       post.addBodyText(ss.toString());
       client.post(url, post);
     }
   } catch (Exception e) {
     e.printStackTrace();
   }
 }
Exemplo n.º 4
0
 private OAuthAccessToken parseJsonToken(HttpMessage response) {
   com.google.gson.JsonObject root = new com.google.gson.JsonObject();
   com.google.gson.JsonParseException pe = null;
   try {
     root =
         (com.google.gson.JsonObject) new com.google.gson.JsonParser().parse(response.getBody());
   } catch (com.google.gson.JsonParseException error) {
     pe = error;
   }
   boolean ok = root != null;
   if (!ok) {
     logger.error(
         new StringWriter().append("parseJsonToken(): ").append(pe.toString()).toString());
     throw new OAuthProcess.TokenError(WString.tr("Wt.Auth.OAuthService.badjson"));
   } else {
     if (response.getStatus() == 200) {
       try {
         String accessToken = root.get("access_token").getAsString();
         int secs = JsonUtils.orIfNullInt(root.get("expires_in"), -1);
         WDate expires = null;
         if (secs > 0) {
           expires = new WDate(new Date()).addSeconds(secs);
         }
         String refreshToken = JsonUtils.orIfNullString(root.get("refreshToken"), "");
         return new OAuthAccessToken(accessToken, expires, refreshToken);
       } catch (RuntimeException e) {
         logger.error(
             new StringWriter().append("token response error: ").append(e.toString()).toString());
         throw new OAuthProcess.TokenError(WString.tr("Wt.Auth.OAuthService.badresponse"));
       }
     } else {
       throw new OAuthProcess.TokenError(
           WString.tr(
               "Wt.Auth.OAuthService."
                   + JsonUtils.orIfNullString(root.get("error"), "missing error")));
     }
   }
 }