Search in sources :

Example 6 with OAuthConsumer

use of net.oauth.OAuthConsumer in project cxf by apache.

the class TemporaryCredentialsController method handleRequest.

@RequestMapping("/handleTemporaryCredentials")
public ModelAndView handleRequest(@ModelAttribute(value = "oAuthParams") OAuthParams oAuthParams, HttpServletResponse response) {
    OAuthServiceProvider provider;
    OAuthConsumer consumer;
    OAuthAccessor accessor;
    OAuthClient client = new OAuthClient(new URLConnectionClient());
    oAuthParams.setErrorMessage(null);
    String temporaryCredentialsEndpointUrl = oAuthParams.getTemporaryCredentialsEndpoint();
    if (temporaryCredentialsEndpointUrl == null || "".equals(temporaryCredentialsEndpointUrl)) {
        oAuthParams.setErrorMessage("Missing temporary credentials endpoint url");
    }
    String clientId = oAuthParams.getClientID();
    if (clientId == null || "".equals(clientId)) {
        oAuthParams.setErrorMessage("Missing client identifier");
    }
    String secret = oAuthParams.getClientSecret();
    if (secret == null || "".equals(secret)) {
        oAuthParams.setErrorMessage("Missing client shared-secret");
    }
    if (oAuthParams.getErrorMessage() == null) {
        provider = new OAuthServiceProvider(temporaryCredentialsEndpointUrl, oAuthParams.getResourceOwnerAuthorizationEndpoint(), oAuthParams.getTokenRequestEndpoint());
        consumer = new OAuthConsumer(null, clientId, secret, provider);
        accessor = new OAuthAccessor(consumer);
        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_CALLBACK, oAuthParams.getCallbackURL());
        parameters.put("realm", "private");
        parameters.put("scope", "read_info modify_info");
        try {
            accessor.consumer.setProperty(OAuthClient.PARAMETER_STYLE, ParameterStyle.AUTHORIZATION_HEADER);
            client.getRequestToken(accessor, OAuthMessage.POST, parameters.entrySet());
        } catch (Exception e) {
            oAuthParams.setErrorMessage(e.toString());
        }
        oAuthParams.setOauthToken(accessor.requestToken);
        oAuthParams.setOauthTokenSecret(accessor.tokenSecret);
        Cookie cId = new Cookie("clientID", oAuthParams.getClientID());
        Cookie cSec = new Cookie("clientSecret", oAuthParams.getClientSecret());
        Cookie tokenSec = new Cookie("tokenSec", accessor.tokenSecret);
        response.addCookie(cId);
        response.addCookie(cSec);
        response.addCookie(tokenSec);
    }
    ModelAndView modelAndView = new ModelAndView();
    if (oAuthParams.getErrorMessage() != null) {
        modelAndView.setViewName("temporaryCredentials");
    } else {
        modelAndView.setViewName("authorizeResourceOwner");
    }
    return modelAndView;
}
Also used : OAuthAccessor(net.oauth.OAuthAccessor) Cookie(javax.servlet.http.Cookie) OAuthServiceProvider(net.oauth.OAuthServiceProvider) URLConnectionClient(net.oauth.client.URLConnectionClient) OAuthClient(net.oauth.client.OAuthClient) HashMap(java.util.HashMap) ModelAndView(org.springframework.web.servlet.ModelAndView) OAuthConsumer(net.oauth.OAuthConsumer) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 7 with OAuthConsumer

use of net.oauth.OAuthConsumer in project bigbluebutton by bigbluebutton.

the class IMSPOXRequest method validateRequest.

// Assumes data is all loaded
public void validateRequest(String oauth_consumer_key, String oauth_secret, HttpServletRequest request) {
    valid = false;
    OAuthMessage oam = OAuthServlet.getMessage(request, null);
    OAuthValidator oav = new SimpleOAuthValidator();
    OAuthConsumer cons = new OAuthConsumer("about:blank#OAuth+CallBack+NotUsed", oauth_consumer_key, oauth_secret, null);
    OAuthAccessor acc = new OAuthAccessor(cons);
    try {
        base_string = OAuthSignatureMethod.getBaseString(oam);
    } catch (Exception e) {
        base_string = null;
    }
    try {
        oav.validateMessage(oam, acc);
    } catch (Exception e) {
        errorMessage = "Launch fails OAuth validation: " + e.getMessage();
        return;
    }
    valid = true;
}
Also used : SimpleOAuthValidator(net.oauth.SimpleOAuthValidator) OAuthAccessor(net.oauth.OAuthAccessor) SimpleOAuthValidator(net.oauth.SimpleOAuthValidator) OAuthValidator(net.oauth.OAuthValidator) OAuthMessage(net.oauth.OAuthMessage) OAuthConsumer(net.oauth.OAuthConsumer) IllegalArgumentException(java.lang.IllegalArgumentException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException)

Example 8 with OAuthConsumer

use of net.oauth.OAuthConsumer in project zm-mailbox by Zimbra.

the class OAuthAccessorSerializer method deserialize.

@Override
public OAuthAccessor deserialize(Object obj) throws ServiceException {
    String value = (String) obj;
    LOG.debug("get value: " + value);
    String consumer_key = value.substring(0, value.indexOf(",token_secret")).substring(13);
    String token_secret = value.substring(value.indexOf(",token_secret"), value.indexOf(",callback")).substring(14);
    String callback = value.substring(value.indexOf(",callback"), value.indexOf(",user")).substring(10);
    String user = value.substring(value.indexOf(",user"), value.indexOf(",authorized")).substring(6);
    String authorized = value.substring(value.indexOf(",authorized"), value.indexOf(",zauthtoken")).substring(12);
    String zauthtoken = value.substring(value.indexOf(",zauthtoken"), value.indexOf(",verifier")).substring(12);
    String verifier = value.substring(value.indexOf(",verifier"), value.indexOf(",approved_on")).substring(10);
    String approved_on = value.substring(value.indexOf(",approved_on"), value.indexOf(",device")).substring(13);
    String device = value.substring(value.indexOf(",device")).substring(8);
    LOG.debug("[consumer_key:%s, callback:%s, user:%s, authorized:%s, zauthtoken:%s, verifier:%s, approved_on:%s, device:%s]", consumer_key, callback, user, authorized, zauthtoken, verifier, approved_on, device);
    try {
        OAuthConsumer consumer = OAuthServiceProvider.getConsumer(consumer_key);
        OAuthAccessor accessor = new OAuthAccessor(consumer);
        accessor.tokenSecret = token_secret;
        accessor.setProperty(OAuth.OAUTH_CALLBACK, callback);
        if (!user.equals("null")) {
            accessor.setProperty("user", user);
        }
        if (authorized.equalsIgnoreCase(Boolean.FALSE.toString())) {
            accessor.setProperty("authorized", Boolean.FALSE);
        } else if (authorized.equalsIgnoreCase(Boolean.TRUE.toString())) {
            accessor.setProperty("authorized", Boolean.TRUE);
        }
        if (!zauthtoken.equals("null")) {
            accessor.setProperty("ZM_AUTH_TOKEN", zauthtoken);
            AuthToken zimbraAuthToken = ZimbraAuthToken.getAuthToken(zauthtoken);
            final Account account = zimbraAuthToken.getAccount();
            OAuthServiceProvider.setAccountPropertiesForAccessor(account, accessor);
        }
        if (!verifier.equals("null")) {
            accessor.setProperty(OAuth.OAUTH_VERIFIER, verifier);
        }
        if (null != approved_on) {
            accessor.consumer.setProperty("approved_on", approved_on);
        }
        if (null != device) {
            accessor.consumer.setProperty("device", device);
        }
        return accessor;
    } catch (Exception e) {
        //need more hack here for hadnling IOException properly
        throw ServiceException.FAILURE("IOException", e);
    }
}
Also used : OAuthAccessor(net.oauth.OAuthAccessor) Account(com.zimbra.cs.account.Account) ZimbraAuthToken(com.zimbra.cs.account.ZimbraAuthToken) AuthToken(com.zimbra.cs.account.AuthToken) OAuthConsumer(net.oauth.OAuthConsumer) ServiceException(com.zimbra.common.service.ServiceException)

Example 9 with OAuthConsumer

use of net.oauth.OAuthConsumer in project zm-mailbox by Zimbra.

the class OAuthServiceProvider method getConsumer.

public static synchronized OAuthConsumer getConsumer(String consumer_key) throws IOException, OAuthProblemException {
    String[] registeredConsumers;
    try {
        // TODO - need to lookup the domain first
        registeredConsumers = Provisioning.getInstance().getConfig().getMultiAttr(Provisioning.A_zimbraOAuthConsumerCredentials);
    } catch (ServiceException e) {
        throw new OAuthProblemException("token_rejected");
    }
    OAuthConsumer oAuthConsumer = null;
    for (String consumer : registeredConsumers) {
        String[] s = consumer.split(":");
        if (s.length >= 2 && s[0].equals(consumer_key)) {
            oAuthConsumer = new OAuthConsumer(null, consumer_key, s[1], null);
            oAuthConsumer.setProperty("key", consumer_key);
            oAuthConsumer.setProperty("app_name", s.length > 2 ? s[2] : "");
            break;
        }
    }
    if (oAuthConsumer == null) {
        throw new OAuthProblemException("token_rejected");
    }
    return oAuthConsumer;
}
Also used : OAuthProblemException(net.oauth.OAuthProblemException) ServiceException(com.zimbra.common.service.ServiceException) OAuthConsumer(net.oauth.OAuthConsumer)

Example 10 with OAuthConsumer

use of net.oauth.OAuthConsumer in project cxf by apache.

the class TokenRequestController method handleRequest.

@RequestMapping("/tokenRequest")
protected ModelAndView handleRequest(@ModelAttribute("oAuthParams") OAuthParams oAuthParams, HttpServletRequest request) throws Exception {
    String oauthToken = oAuthParams.getOauthToken();
    String tokenRequestEndpoint = oAuthParams.getTokenRequestEndpoint();
    String clientID = oAuthParams.getClientID();
    if (tokenRequestEndpoint == null || "".equals(tokenRequestEndpoint)) {
        oAuthParams.setErrorMessage("Missing token request URI");
    }
    if (clientID == null || "".equals(clientID)) {
        oAuthParams.setErrorMessage("Missing consumer key");
    }
    if (oauthToken == null || "".equals(oauthToken)) {
        oAuthParams.setErrorMessage("Missing oauth token");
    }
    String verifier = oAuthParams.getOauthVerifier();
    if (verifier == null || "".equals(verifier)) {
        oAuthParams.setErrorMessage("Missing oauth verifier");
    }
    if (oAuthParams.getErrorMessage() == null) {
        OAuthClient client = new OAuthClient(new URLConnectionClient());
        OAuthServiceProvider provider = new OAuthServiceProvider(oAuthParams.getTemporaryCredentialsEndpoint(), oAuthParams.getResourceOwnerAuthorizationEndpoint(), tokenRequestEndpoint);
        OAuthConsumer consumer = new OAuthConsumer(null, clientID, oAuthParams.getClientSecret(), provider);
        OAuthAccessor accessor = new OAuthAccessor(consumer);
        accessor.requestToken = oauthToken;
        accessor.tokenSecret = Common.findCookieValue(request, "tokenSec");
        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, oauthToken);
        parameters.put(OAuth.OAUTH_VERIFIER, oAuthParams.getOauthVerifier());
        try {
            client.getAccessToken(accessor, OAuthMessage.GET, parameters.entrySet());
            oAuthParams.setOauthToken(accessor.accessToken);
        } catch (Exception e) {
            oAuthParams.setErrorMessage(e.toString());
            oAuthParams.setOauthToken(oauthToken);
            return new ModelAndView("tokenRequest");
        }
        oAuthParams.setOauthTokenSecret(accessor.tokenSecret);
    }
    oAuthParams.setClientID(Common.findCookieValue(request, "clientID"));
    oAuthParams.setClientSecret(Common.findCookieValue(request, "clientSecret"));
    return new ModelAndView("accessToken");
}
Also used : OAuthAccessor(net.oauth.OAuthAccessor) OAuthServiceProvider(net.oauth.OAuthServiceProvider) URLConnectionClient(net.oauth.client.URLConnectionClient) OAuthClient(net.oauth.client.OAuthClient) HashMap(java.util.HashMap) ModelAndView(org.springframework.web.servlet.ModelAndView) OAuthConsumer(net.oauth.OAuthConsumer) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Aggregations

OAuthConsumer (net.oauth.OAuthConsumer)10 OAuthAccessor (net.oauth.OAuthAccessor)9 OAuthMessage (net.oauth.OAuthMessage)5 HashMap (java.util.HashMap)4 OAuthServiceProvider (net.oauth.OAuthServiceProvider)3 OAuthClient (net.oauth.client.OAuthClient)3 URLConnectionClient (net.oauth.client.URLConnectionClient)3 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)3 ModelAndView (org.springframework.web.servlet.ModelAndView)3 ServiceException (com.zimbra.common.service.ServiceException)2 IOException (java.io.IOException)2 IllegalArgumentException (java.lang.IllegalArgumentException)2 OAuthProblemException (net.oauth.OAuthProblemException)2 OAuthValidator (net.oauth.OAuthValidator)2 SimpleOAuthValidator (net.oauth.SimpleOAuthValidator)2 Account (com.zimbra.cs.account.Account)1 AuthToken (com.zimbra.cs.account.AuthToken)1 ZimbraAuthToken (com.zimbra.cs.account.ZimbraAuthToken)1 OutputStream (java.io.OutputStream)1 Map (java.util.Map)1