use of net.oauth.OAuthMessage in project zm-mailbox by Zimbra.
the class OAuthAccessTokenServlet method processRequest.
public void processRequest(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
try {
String origUrl = request.getHeader("X-Zimbra-Orig-Url");
OAuthMessage oAuthMessage = StringUtil.isNullOrEmpty(origUrl) ? OAuthServlet.getMessage(request, null) : OAuthServlet.getMessage(request, origUrl);
OAuthAccessor accessor = OAuthServiceProvider.getAccessor(oAuthMessage);
OAuthServiceProvider.VALIDATOR.validateAccTokenMessage(oAuthMessage, accessor);
// make sure token is authorized
if (!Boolean.TRUE.equals(accessor.getProperty("authorized"))) {
OAuthProblemException problem = new OAuthProblemException("permission_denied");
LOG.debug("permission_denied");
throw problem;
}
AuthToken userAuthToken = ZimbraAuthToken.getAuthToken((String) accessor.getProperty("ZM_AUTH_TOKEN"));
String accountId = userAuthToken.getAccountId();
Account account = Provisioning.getInstance().getAccountById(accountId);
// generate access token and secret
OAuthServiceProvider.generateAccessToken(accessor);
account.addForeignPrincipal("oAuthAccessToken:" + accessor.accessToken);
account.addOAuthAccessor(accessor.accessToken + "::" + new OAuthAccessorSerializer().serialize(accessor));
response.setContentType("text/plain");
OutputStream out = response.getOutputStream();
OAuth.formEncode(OAuth.newList("oauth_token", accessor.accessToken, "oauth_token_secret", accessor.tokenSecret), out);
out.close();
} catch (Exception e) {
LOG.debug("AccessTokenHandler exception", e);
OAuthServiceProvider.handleException(e, request, response, true);
}
}
use of net.oauth.OAuthMessage in project zm-mailbox by Zimbra.
the class OAuthRequestTokenServlet method processRequest.
public void processRequest(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
try {
OAuthMessage oAuthMessage = OAuthServlet.getMessage(request, null);
OAuthConsumer consumer = OAuthServiceProvider.getConsumer(oAuthMessage);
//TODO: This property is applicable for mobile client.
//For other types of consumers there will be more fields(e.g. Browser)
String device = request.getParameter("device");
consumer.setProperty("device", device);
OAuthAccessor accessor = new OAuthAccessor(consumer);
OAuthServiceProvider.VALIDATOR.validateReqTokenMessage(oAuthMessage, accessor);
// generate request_token and secret
OAuthServiceProvider.generateRequestToken(accessor);
response.setContentType("text/plain");
OutputStream out = response.getOutputStream();
OAuth.formEncode(OAuth.newList("oauth_token", accessor.requestToken, "oauth_token_secret", accessor.tokenSecret, OAuth.OAUTH_CALLBACK_CONFIRMED, "true"), out);
out.close();
} catch (Exception e) {
LOG.debug("RequestTokenHandler exception", e);
OAuthServiceProvider.handleException(e, request, response, true);
}
}
use of net.oauth.OAuthMessage 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 net.oauth.OAuthMessage in project cxf by apache.
the class OAuthUtils method getOAuthMessage.
public static OAuthMessage getOAuthMessage(MessageContext mc, HttpServletRequest request, String[] requiredParams) throws Exception {
OAuthMessage oAuthMessage = OAuthServlet.getMessage(request, request.getRequestURL().toString());
OAuthUtils.addParametersIfNeeded(mc, request, oAuthMessage);
oAuthMessage.requireParameters(requiredParams);
return oAuthMessage;
}
use of net.oauth.OAuthMessage in project cxf by apache.
the class GetProtectedResourceController method handleRequest.
@RequestMapping("/getProtectedResource")
protected ModelAndView handleRequest(@ModelAttribute("oAuthParams") OAuthParams oAuthParams, HttpServletRequest request) throws Exception {
OAuthServiceProvider provider = new OAuthServiceProvider(oAuthParams.getTemporaryCredentialsEndpoint(), oAuthParams.getResourceOwnerAuthorizationEndpoint(), null);
OAuthConsumer consumer = new OAuthConsumer(null, oAuthParams.getClientID(), oAuthParams.getClientSecret(), provider);
OAuthAccessor accessor = new OAuthAccessor(consumer);
accessor.requestToken = oAuthParams.getOauthToken();
accessor.tokenSecret = oAuthParams.getOauthTokenSecret();
Map<String, String> parameters = new HashMap<>();
parameters.put(OAuth.OAUTH_SIGNATURE_METHOD, oAuthParams.getSignatureMethod());
parameters.put(OAuth.OAUTH_NONCE, UUID.randomUUID().toString());
parameters.put(OAuth.OAUTH_TIMESTAMP, String.valueOf(System.currentTimeMillis() / 1000));
parameters.put(OAuth.OAUTH_TOKEN, oAuthParams.getOauthToken());
parameters.put(OAuth.OAUTH_CONSUMER_KEY, oAuthParams.getClientID());
OAuthMessage msg = null;
String method = request.getParameter("op");
if ("GET".equals(method)) {
msg = accessor.newRequestMessage(OAuthMessage.GET, oAuthParams.getGetResourceURL(), parameters.entrySet());
} else {
msg = accessor.newRequestMessage(OAuthMessage.POST, oAuthParams.getPostResourceURL(), parameters.entrySet());
}
OAuthClient client = new OAuthClient(new URLConnectionClient());
msg = client.access(msg, ParameterStyle.QUERY_STRING);
StringBuilder bodyBuffer = readBody(msg);
oAuthParams.setResourceResponse(bodyBuffer.toString());
String authHeader = msg.getHeader("WWW-Authenticate");
String oauthHeader = msg.getHeader("OAuth");
String header = "";
if (authHeader != null) {
header += "WWW-Authenticate:" + authHeader;
}
if (oauthHeader != null) {
header += "OAuth:" + oauthHeader;
}
oAuthParams.setHeader(header);
oAuthParams.setResponseCode(((OAuthResponseMessage) msg).getHttpResponse().getStatusCode());
return new ModelAndView("accessToken");
}
Aggregations