use of org.apache.cxf.rs.security.oauth.data.RequestToken in project cxf by apache.
the class AccessTokenHandler method handle.
public Response handle(MessageContext mc, OAuthDataProvider dataProvider, OAuthValidator validator) {
try {
OAuthMessage oAuthMessage = OAuthUtils.getOAuthMessage(mc, mc.getHttpServletRequest(), REQUIRED_PARAMETERS);
RequestToken requestToken = dataProvider.getRequestToken(oAuthMessage.getToken());
if (requestToken == null) {
throw new OAuthProblemException(OAuth.Problems.TOKEN_REJECTED);
}
String oauthVerifier = oAuthMessage.getParameter(OAuth.OAUTH_VERIFIER);
if (StringUtils.isEmpty(oauthVerifier)) {
if (requestToken.getSubject() != null && requestToken.isPreAuthorized()) {
LOG.fine("Preauthorized request token");
} else {
throw new OAuthProblemException(OAuthConstants.VERIFIER_INVALID);
}
} else if (!oauthVerifier.equals(requestToken.getVerifier())) {
throw new OAuthProblemException(OAuthConstants.VERIFIER_INVALID);
}
OAuthUtils.validateMessage(oAuthMessage, requestToken.getClient(), requestToken, dataProvider, validator);
AccessTokenRegistration reg = new AccessTokenRegistration();
reg.setRequestToken(requestToken);
AccessToken accessToken = dataProvider.createAccessToken(reg);
// create response
Map<String, Object> responseParams = new HashMap<>();
responseParams.put(OAuth.OAUTH_TOKEN, accessToken.getTokenKey());
responseParams.put(OAuth.OAUTH_TOKEN_SECRET, accessToken.getTokenSecret());
String responseString = OAuth.formEncode(responseParams.entrySet());
return Response.ok(responseString).build();
} catch (OAuthProblemException e) {
LOG.log(Level.WARNING, "An OAuth-related problem: {0}", new Object[] { e.fillInStackTrace() });
int code = e.getHttpStatusCode();
if (code == HttpServletResponse.SC_OK) {
code = e.getProblem() == OAuth.Problems.CONSUMER_KEY_UNKNOWN ? 401 : 400;
}
return OAuthUtils.handleException(mc, e, code);
} catch (OAuthServiceException e) {
return OAuthUtils.handleException(mc, e, HttpServletResponse.SC_BAD_REQUEST);
} catch (Exception e) {
LOG.log(Level.SEVERE, "Unexpected internal server exception: {0}", new Object[] { e.fillInStackTrace() });
return OAuthUtils.handleException(mc, e, HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
}
}
use of org.apache.cxf.rs.security.oauth.data.RequestToken in project cxf by apache.
the class OAuthUtils method validateMessage.
public static void validateMessage(OAuthMessage oAuthMessage, Client client, Token token, OAuthDataProvider provider, OAuthValidator validator) throws Exception {
OAuthConsumer consumer = new OAuthConsumer(null, client.getConsumerKey(), client.getSecretKey(), null);
OAuthAccessor accessor = new OAuthAccessor(consumer);
if (token != null) {
if (token instanceof RequestToken) {
accessor.requestToken = token.getTokenKey();
} else {
accessor.accessToken = token.getTokenKey();
}
accessor.tokenSecret = token.getTokenSecret();
}
try {
validator.validateMessage(oAuthMessage, accessor);
} catch (Exception ex) {
if (token != null) {
provider.removeToken(token);
}
throw ex;
}
if (token != null && validator instanceof DefaultOAuthValidator) {
((DefaultOAuthValidator) validator).validateToken(token, provider);
}
}
use of org.apache.cxf.rs.security.oauth.data.RequestToken in project cxf by apache.
the class MemoryOAuthDataProvider method finalizeAuthorization.
public String finalizeAuthorization(AuthorizationInput input) throws OAuthServiceException {
RequestToken requestToken = input.getToken();
requestToken.setVerifier(generateToken());
return requestToken.getVerifier();
}
use of org.apache.cxf.rs.security.oauth.data.RequestToken in project cxf by apache.
the class MemoryOAuthDataProvider method createRequestToken.
public RequestToken createRequestToken(RequestTokenRegistration reg) throws OAuthServiceException {
String token = generateToken();
String tokenSecret = generateToken();
RequestToken reqToken = new RequestToken(reg.getClient(), token, tokenSecret, reg.getLifetime(), reg.getIssuedAt());
reqToken.setScopes(getPermissionsInfo(reg.getScopes()));
reqToken.setCallback(reg.getCallback());
oauthTokens.put(token, reqToken);
return reqToken;
}
use of org.apache.cxf.rs.security.oauth.data.RequestToken in project cxf by apache.
the class MemoryOAuthDataProvider method createRequestToken.
public RequestToken createRequestToken(RequestTokenRegistration reg) throws OAuthServiceException {
String token = generateToken();
String tokenSecret = generateToken();
RequestToken reqToken = new RequestToken(reg.getClient(), token, tokenSecret, reg.getLifetime(), reg.getIssuedAt());
reqToken.setScopes(getPermissionsInfo(reg.getScopes()));
oauthTokens.put(token, reqToken);
return reqToken;
}
Aggregations