Package com.nimbusds.oauth2.sdk.http

Examples of com.nimbusds.oauth2.sdk.http.HTTPResponse



  public void testParseFromHTTPResponseWithCustomParams()
    throws Exception {

    HTTPResponse httpResponse = new HTTPResponse(HTTPResponse.SC_OK);
    httpResponse.setContentType(CommonContentTypes.APPLICATION_JSON);
    httpResponse.setCacheControl("no-store");
    httpResponse.setPragma("no-cache");

    JSONObject o = new JSONObject();

    final String accessTokenString = "SlAV32hkKG";
    o.put("access_token", accessTokenString);

    o.put("token_type", "Bearer");

    final String refreshTokenString = "8xLOxBtZp8";
    o.put("refresh_token", refreshTokenString);

    final long exp = 3600;
    o.put("expires_in", exp);

    o.put("sub_sid", "abc");
    o.put("priority", 10);

    httpResponse.setContent(o.toString());


    AccessTokenResponse atr = AccessTokenResponse.parse(httpResponse);

    AccessToken accessToken = atr.getAccessToken();
    assertEquals(accessTokenString, accessToken.getValue());
    assertEquals(exp, accessToken.getLifetime());
    assertNull(accessToken.getScope());

    RefreshToken refreshToken = atr.getRefreshToken();
    assertEquals(refreshTokenString, refreshToken.getValue());

    // Custom param
    assertEquals("abc", (String)atr.getCustomParams().get("sub_sid"));
    assertEquals(10, ((Number)atr.getCustomParams().get("priority")).intValue());
    assertEquals(2, atr.getCustomParams().size());

    // Test pair getter
    TokenPair pair = atr.getTokenPair();
    assertEquals(accessToken, pair.getAccessToken());
    assertEquals(refreshToken, pair.getRefreshToken());

    httpResponse = atr.toHTTPResponse();

    assertEquals(CommonContentTypes.APPLICATION_JSON, httpResponse.getContentType());
    assertEquals("no-store", httpResponse.getCacheControl());
    assertEquals("no-cache", httpResponse.getPragma());

    o = httpResponse.getContentAsJSONObject();

    assertEquals(accessTokenString, o.get("access_token"));
    assertEquals("Bearer", o.get("token_type"));
    assertEquals(refreshTokenString, o.get("refresh_token"));
    assertEquals(3600l, o.get("expires_in"));
View Full Code Here



  public void testParseFromAltHTTPResponse()
    throws Exception {

    HTTPResponse httpResponse = new HTTPResponse(HTTPResponse.SC_OK);
    httpResponse.setContentType(CommonContentTypes.APPLICATION_JSON);
    httpResponse.setCacheControl("no-store");
    httpResponse.setPragma("no-cache");

    JSONObject o = new JSONObject();

    final String accessTokenString = "SlAV32hkKG";
    o.put("access_token", accessTokenString);

    o.put("token_type", "bearer");

    httpResponse.setContent(o.toString());


    AccessTokenResponse atr = AccessTokenResponse.parse(httpResponse);

    AccessToken accessToken = atr.getAccessToken();
    assertEquals(accessTokenString, accessToken.getValue());
    assertNull(accessToken.getScope());

    // Test pair getter
    TokenPair pair = atr.getTokenPair();
    assertEquals(accessToken, pair.getAccessToken());

    httpResponse = atr.toHTTPResponse();

    assertEquals(CommonContentTypes.APPLICATION_JSON, httpResponse.getContentType());
    assertEquals("no-store", httpResponse.getCacheControl());
    assertEquals("no-cache", httpResponse.getPragma());

    o = httpResponse.getContentAsJSONObject();

    assertEquals(accessTokenString, o.get("access_token"));
    assertEquals("Bearer", o.get("token_type"));
  }
View Full Code Here

   * @return The HTTP response.
   */
  @Override
  public HTTPResponse toHTTPResponse() {

    HTTPResponse httpResponse;

    if (error.getHTTPStatusCode() > 0) {
      httpResponse = new HTTPResponse(error.getHTTPStatusCode());
    } else {
      httpResponse = new HTTPResponse(HTTPResponse.SC_BAD_REQUEST);
    }

    // Add the WWW-Authenticate header
    if (error instanceof BearerTokenError) {

      BearerTokenError bte = (BearerTokenError)error;

      httpResponse.setWWWAuthenticate(bte.toWWWAuthenticateHeader());

    } else {
      JSONObject jsonObject = new JSONObject();

      if (error.getCode() != null)
        jsonObject.put("error", error.getCode());

      if (error.getDescription() != null)
        jsonObject.put("error_description", error.getDescription());

      httpResponse.setContentType(CommonContentTypes.APPLICATION_JSON);

      httpResponse.setContent(jsonObject.toString());
    }
   
    httpResponse.setCacheControl("no-store");
    httpResponse.setPragma("no-cache");

    return httpResponse;
  }
View Full Code Here


  @Override
  public HTTPResponse toHTTPResponse() {
 
    HTTPResponse httpResponse = new HTTPResponse(HTTPResponse.SC_OK);
    httpResponse.setContentType(CommonContentTypes.APPLICATION_JSON);
    httpResponse.setCacheControl("no-store");
    httpResponse.setPragma("no-cache");
    httpResponse.setContent(clientInfo.toJSONObject().toString());
    return httpResponse;
  }
View Full Code Here

    OIDCClientInformation clientInfo = new OIDCClientInformation(id, null, metadata, null, regURI, accessToken);

    OIDCClientInformationResponse response = new OIDCClientInformationResponse(clientInfo);

    HTTPResponse httpResponse = response.toHTTPResponse();

    ClientRegistrationResponse regResponse = OIDCClientRegistrationResponseParser.parse(httpResponse);

    response = (OIDCClientInformationResponse)regResponse;
View Full Code Here

  public void testParseError()
    throws Exception {

    ClientRegistrationErrorResponse response = new ClientRegistrationErrorResponse(BearerTokenError.INVALID_TOKEN);

    HTTPResponse httpResponse = response.toHTTPResponse();

    ClientRegistrationResponse regResponse = OIDCClientRegistrationResponseParser.parse(httpResponse);

    response = (ClientRegistrationErrorResponse)regResponse;
    assertEquals(BearerTokenError.INVALID_TOKEN.getCode(), response.getErrorObject().getCode());
View Full Code Here

    OIDCClientInformationResponse response = new OIDCClientInformationResponse(info);

    assertEquals(info, response.getOIDCClientInformation());
    assertEquals(info, response.getClientInformation());

    HTTPResponse httpResponse = response.toHTTPResponse();

    response = OIDCClientInformationResponse.parse(httpResponse);

    assertEquals(id.getValue(), response.getClientInformation().getID().getValue());
    assertEquals(issueDate, response.getClientInformation().getIDIssueDate());
View Full Code Here


  public void testToHTTPResponse()
    throws Exception {

    HTTPResponse httpResponse =
      new ClientRegistrationErrorResponse(RegistrationError.INVALID_CLIENT_METADATA).toHTTPResponse();

    assertEquals(400, httpResponse.getStatusCode());
    assertTrue(CommonContentTypes.APPLICATION_JSON.match(httpResponse.getContentType()));
    JSONObject content = httpResponse.getContentAsJSONObject();
    assertEquals("invalid_client_metadata", (String)content.get("error"));
    assertEquals("Invalid client metadata field", (String)content.get("error_description"));

    assertEquals("no-store", httpResponse.getCacheControl());
    assertEquals("no-cache", httpResponse.getPragma());
  }
View Full Code Here


  public void testParse()
    throws Exception {

    HTTPResponse httpResponse =
      new ClientRegistrationErrorResponse(RegistrationError.INVALID_CLIENT_METADATA).toHTTPResponse();

    ClientRegistrationErrorResponse errorResponse =
      ClientRegistrationErrorResponse.parse(httpResponse);
View Full Code Here

   */
  @Override
  public HTTPResponse toHTTPResponse()
    throws SerializeException {
 
    HTTPResponse response = new HTTPResponse(HTTPResponse.SC_FOUND);
    response.setLocation(toURI());
    return response;
  }
View Full Code Here

TOP

Related Classes of com.nimbusds.oauth2.sdk.http.HTTPResponse

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.