Return-Path: X-Original-To: apmail-cxf-commits-archive@www.apache.org Delivered-To: apmail-cxf-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 901D6D3C9 for ; Fri, 13 Jul 2012 12:11:01 +0000 (UTC) Received: (qmail 13501 invoked by uid 500); 13 Jul 2012 12:11:01 -0000 Delivered-To: apmail-cxf-commits-archive@cxf.apache.org Received: (qmail 13364 invoked by uid 500); 13 Jul 2012 12:11:00 -0000 Mailing-List: contact commits-help@cxf.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@cxf.apache.org Delivered-To: mailing list commits@cxf.apache.org Received: (qmail 13322 invoked by uid 99); 13 Jul 2012 12:10:59 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 13 Jul 2012 12:10:59 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 13 Jul 2012 12:10:41 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id B33A523888E7; Fri, 13 Jul 2012 12:10:15 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1361175 - in /cxf/trunk/rt/rs/security/oauth-parent/oauth2/src/main/java/org/apache/cxf/rs/security/oauth2: grants/owner/ services/ utils/ Date: Fri, 13 Jul 2012 12:10:15 -0000 To: commits@cxf.apache.org From: sergeyb@apache.org X-Mailer: svnmailer-1.0.8-patched Message-Id: <20120713121015.B33A523888E7@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: sergeyb Date: Fri Jul 13 12:10:14 2012 New Revision: 1361175 URL: http://svn.apache.org/viewvc?rev=1361175&view=rev Log: [CXF-4419] Prototyping a resource owner grant handler Added: cxf/trunk/rt/rs/security/oauth-parent/oauth2/src/main/java/org/apache/cxf/rs/security/oauth2/grants/owner/ cxf/trunk/rt/rs/security/oauth-parent/oauth2/src/main/java/org/apache/cxf/rs/security/oauth2/grants/owner/JAASResourceOwnerLoginHandler.java (with props) cxf/trunk/rt/rs/security/oauth-parent/oauth2/src/main/java/org/apache/cxf/rs/security/oauth2/grants/owner/ResourceOwnerGrantHandler.java (with props) cxf/trunk/rt/rs/security/oauth-parent/oauth2/src/main/java/org/apache/cxf/rs/security/oauth2/grants/owner/ResourceOwnerLoginHandler.java (with props) Modified: cxf/trunk/rt/rs/security/oauth-parent/oauth2/src/main/java/org/apache/cxf/rs/security/oauth2/services/RedirectionBasedGrantService.java cxf/trunk/rt/rs/security/oauth-parent/oauth2/src/main/java/org/apache/cxf/rs/security/oauth2/utils/OAuthConstants.java cxf/trunk/rt/rs/security/oauth-parent/oauth2/src/main/java/org/apache/cxf/rs/security/oauth2/utils/OAuthUtils.java Added: cxf/trunk/rt/rs/security/oauth-parent/oauth2/src/main/java/org/apache/cxf/rs/security/oauth2/grants/owner/JAASResourceOwnerLoginHandler.java URL: http://svn.apache.org/viewvc/cxf/trunk/rt/rs/security/oauth-parent/oauth2/src/main/java/org/apache/cxf/rs/security/oauth2/grants/owner/JAASResourceOwnerLoginHandler.java?rev=1361175&view=auto ============================================================================== --- cxf/trunk/rt/rs/security/oauth-parent/oauth2/src/main/java/org/apache/cxf/rs/security/oauth2/grants/owner/JAASResourceOwnerLoginHandler.java (added) +++ cxf/trunk/rt/rs/security/oauth-parent/oauth2/src/main/java/org/apache/cxf/rs/security/oauth2/grants/owner/JAASResourceOwnerLoginHandler.java Fri Jul 13 12:10:14 2012 @@ -0,0 +1,61 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.cxf.rs.security.oauth2.grants.owner; + +import org.apache.cxf.configuration.security.AuthorizationPolicy; +import org.apache.cxf.interceptor.security.JAASLoginInterceptor; +import org.apache.cxf.message.Message; +import org.apache.cxf.message.MessageImpl; +import org.apache.cxf.rs.security.oauth2.common.UserSubject; +import org.apache.cxf.rs.security.oauth2.utils.OAuthUtils; +import org.apache.cxf.security.SecurityContext; + +public class JAASResourceOwnerLoginHandler implements ResourceOwnerLoginHandler { + + private JAASLoginInterceptor jaasInterceptor = new JAASLoginInterceptor(); + + + public UserSubject createSubject(String name, String password) { + Message message = setupMessage(name, password); + jaasInterceptor.handleMessage(message); + + return OAuthUtils.createSubject(message.get(SecurityContext.class)); + } + + public void setContextName(String name) { + jaasInterceptor.setContextName(name); + } + + public void setRoleClassifier(String value) { + jaasInterceptor.setRoleClassifier(value); + } + + public void setRoleClassifierType(String value) { + jaasInterceptor.setRoleClassifierType(value); + } + + private Message setupMessage(String name, String password) { + AuthorizationPolicy policy = new AuthorizationPolicy(); + policy.setUserName(name); + policy.setPassword(password); + Message message = new MessageImpl(); + message.put(AuthorizationPolicy.class, policy); + return message; + } +} Propchange: cxf/trunk/rt/rs/security/oauth-parent/oauth2/src/main/java/org/apache/cxf/rs/security/oauth2/grants/owner/JAASResourceOwnerLoginHandler.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: cxf/trunk/rt/rs/security/oauth-parent/oauth2/src/main/java/org/apache/cxf/rs/security/oauth2/grants/owner/JAASResourceOwnerLoginHandler.java ------------------------------------------------------------------------------ svn:keywords = Rev Date Added: cxf/trunk/rt/rs/security/oauth-parent/oauth2/src/main/java/org/apache/cxf/rs/security/oauth2/grants/owner/ResourceOwnerGrantHandler.java URL: http://svn.apache.org/viewvc/cxf/trunk/rt/rs/security/oauth-parent/oauth2/src/main/java/org/apache/cxf/rs/security/oauth2/grants/owner/ResourceOwnerGrantHandler.java?rev=1361175&view=auto ============================================================================== --- cxf/trunk/rt/rs/security/oauth-parent/oauth2/src/main/java/org/apache/cxf/rs/security/oauth2/grants/owner/ResourceOwnerGrantHandler.java (added) +++ cxf/trunk/rt/rs/security/oauth-parent/oauth2/src/main/java/org/apache/cxf/rs/security/oauth2/grants/owner/ResourceOwnerGrantHandler.java Fri Jul 13 12:10:14 2012 @@ -0,0 +1,68 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.cxf.rs.security.oauth2.grants.owner; + +import javax.ws.rs.core.MultivaluedMap; + +import org.apache.cxf.rs.security.oauth2.common.Client; +import org.apache.cxf.rs.security.oauth2.common.ServerAccessToken; +import org.apache.cxf.rs.security.oauth2.common.UserSubject; +import org.apache.cxf.rs.security.oauth2.grants.AbstractGrantHandler; +import org.apache.cxf.rs.security.oauth2.provider.OAuthServiceException; +import org.apache.cxf.rs.security.oauth2.utils.OAuthConstants; +import org.apache.cxf.rs.security.oauth2.utils.OAuthUtils; + +/** + * The "resource owner" grant handler + */ +public class ResourceOwnerGrantHandler extends AbstractGrantHandler { + private ResourceOwnerLoginHandler loginHandler; + + public ResourceOwnerGrantHandler() { + super(OAuthConstants.RESOURCE_OWNER_GRANT, true); + } + + public ServerAccessToken createAccessToken(Client client, MultivaluedMap params) + throws OAuthServiceException { + checkIfGrantSupported(client); + + String ownerName = params.getFirst(OAuthConstants.RESOURCE_OWNER_NAME); + String ownerPassword = params.getFirst(OAuthConstants.RESOURCE_OWNER_PASSWORD); + if (ownerName == null || ownerPassword == null) { + throw new OAuthServiceException(OAuthConstants.UNAUTHORIZED_CLIENT); + } + + UserSubject subject = null; + try { + subject = loginHandler.createSubject(ownerName, ownerPassword); + } catch (Exception ex) { + throw new OAuthServiceException(OAuthConstants.UNAUTHORIZED_CLIENT); + } + + return doCreateAccessToken(client, + subject, + OAuthUtils.parseScope(params.getFirst(OAuthConstants.SCOPE))); + } + + public void setLoginHandler(ResourceOwnerLoginHandler loginHandler) { + this.loginHandler = loginHandler; + } + + +} Propchange: cxf/trunk/rt/rs/security/oauth-parent/oauth2/src/main/java/org/apache/cxf/rs/security/oauth2/grants/owner/ResourceOwnerGrantHandler.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: cxf/trunk/rt/rs/security/oauth-parent/oauth2/src/main/java/org/apache/cxf/rs/security/oauth2/grants/owner/ResourceOwnerGrantHandler.java ------------------------------------------------------------------------------ svn:keywords = Rev Date Added: cxf/trunk/rt/rs/security/oauth-parent/oauth2/src/main/java/org/apache/cxf/rs/security/oauth2/grants/owner/ResourceOwnerLoginHandler.java URL: http://svn.apache.org/viewvc/cxf/trunk/rt/rs/security/oauth-parent/oauth2/src/main/java/org/apache/cxf/rs/security/oauth2/grants/owner/ResourceOwnerLoginHandler.java?rev=1361175&view=auto ============================================================================== --- cxf/trunk/rt/rs/security/oauth-parent/oauth2/src/main/java/org/apache/cxf/rs/security/oauth2/grants/owner/ResourceOwnerLoginHandler.java (added) +++ cxf/trunk/rt/rs/security/oauth-parent/oauth2/src/main/java/org/apache/cxf/rs/security/oauth2/grants/owner/ResourceOwnerLoginHandler.java Fri Jul 13 12:10:14 2012 @@ -0,0 +1,25 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.cxf.rs.security.oauth2.grants.owner; + +import org.apache.cxf.rs.security.oauth2.common.UserSubject; + +public interface ResourceOwnerLoginHandler { + UserSubject createSubject(String name, String password); +} Propchange: cxf/trunk/rt/rs/security/oauth-parent/oauth2/src/main/java/org/apache/cxf/rs/security/oauth2/grants/owner/ResourceOwnerLoginHandler.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: cxf/trunk/rt/rs/security/oauth-parent/oauth2/src/main/java/org/apache/cxf/rs/security/oauth2/grants/owner/ResourceOwnerLoginHandler.java ------------------------------------------------------------------------------ svn:keywords = Rev Date Modified: cxf/trunk/rt/rs/security/oauth-parent/oauth2/src/main/java/org/apache/cxf/rs/security/oauth2/services/RedirectionBasedGrantService.java URL: http://svn.apache.org/viewvc/cxf/trunk/rt/rs/security/oauth-parent/oauth2/src/main/java/org/apache/cxf/rs/security/oauth2/services/RedirectionBasedGrantService.java?rev=1361175&r1=1361174&r2=1361175&view=diff ============================================================================== --- cxf/trunk/rt/rs/security/oauth-parent/oauth2/src/main/java/org/apache/cxf/rs/security/oauth2/services/RedirectionBasedGrantService.java (original) +++ cxf/trunk/rt/rs/security/oauth-parent/oauth2/src/main/java/org/apache/cxf/rs/security/oauth2/services/RedirectionBasedGrantService.java Fri Jul 13 12:10:14 2012 @@ -19,12 +19,9 @@ package org.apache.cxf.rs.security.oauth2.services; -import java.security.Principal; -import java.util.ArrayList; import java.util.Collections; import java.util.LinkedList; import java.util.List; -import java.util.Set; import java.util.UUID; import javax.servlet.http.HttpSession; @@ -46,7 +43,6 @@ import org.apache.cxf.rs.security.oauth2 import org.apache.cxf.rs.security.oauth2.provider.OAuthServiceException; import org.apache.cxf.rs.security.oauth2.utils.OAuthConstants; import org.apache.cxf.rs.security.oauth2.utils.OAuthUtils; -import org.apache.cxf.security.LoginSecurityContext; import org.apache.cxf.security.SecurityContext; @@ -239,16 +235,7 @@ public abstract class RedirectionBasedGr } private UserSubject createUserSubject(SecurityContext securityContext) { - List roleNames = Collections.emptyList(); - if (securityContext instanceof LoginSecurityContext) { - roleNames = new ArrayList(); - Set roles = ((LoginSecurityContext)securityContext).getUserRoles(); - for (Principal p : roles) { - roleNames.add(p.getName()); - } - } - return - new UserSubject(securityContext.getUserPrincipal().getName(), roleNames); + return OAuthUtils.createSubject(securityContext); } protected abstract Response createErrorResponse(MultivaluedMap params, Modified: cxf/trunk/rt/rs/security/oauth-parent/oauth2/src/main/java/org/apache/cxf/rs/security/oauth2/utils/OAuthConstants.java URL: http://svn.apache.org/viewvc/cxf/trunk/rt/rs/security/oauth-parent/oauth2/src/main/java/org/apache/cxf/rs/security/oauth2/utils/OAuthConstants.java?rev=1361175&r1=1361174&r2=1361175&view=diff ============================================================================== --- cxf/trunk/rt/rs/security/oauth-parent/oauth2/src/main/java/org/apache/cxf/rs/security/oauth2/utils/OAuthConstants.java (original) +++ cxf/trunk/rt/rs/security/oauth-parent/oauth2/src/main/java/org/apache/cxf/rs/security/oauth2/utils/OAuthConstants.java Fri Jul 13 12:10:14 2012 @@ -41,8 +41,8 @@ public final class OAuthConstants { public static final String AUTHORIZATION_CODE_GRANT = "authorization_code"; public static final String CLIENT_CREDENTIALS_GRANT = "client_credentials"; public static final String IMPLICIT_GRANT = "implicit"; + public static final String RESOURCE_OWNER_GRANT = "password"; public static final String REFRESH_TOKEN_GRANT = "refresh_token"; - // etc // Well-known token types public static final String BEARER_TOKEN_TYPE = "bearer"; @@ -64,6 +64,10 @@ public final class OAuthConstants { public static final String AUTHORIZATION_DECISION_ALLOW = "allow"; public static final String AUTHORIZATION_DECISION_DENY = "deny"; + // Resource Owner grant constants + public static final String RESOURCE_OWNER_NAME = "username"; + public static final String RESOURCE_OWNER_PASSWORD = "password"; + // Error constants public static final String ERROR_KEY = "error"; public static final String ERROR_DESCRIPTION_KEY = "error_description"; Modified: cxf/trunk/rt/rs/security/oauth-parent/oauth2/src/main/java/org/apache/cxf/rs/security/oauth2/utils/OAuthUtils.java URL: http://svn.apache.org/viewvc/cxf/trunk/rt/rs/security/oauth-parent/oauth2/src/main/java/org/apache/cxf/rs/security/oauth2/utils/OAuthUtils.java?rev=1361175&r1=1361174&r2=1361175&view=diff ============================================================================== --- cxf/trunk/rt/rs/security/oauth-parent/oauth2/src/main/java/org/apache/cxf/rs/security/oauth2/utils/OAuthUtils.java (original) +++ cxf/trunk/rt/rs/security/oauth-parent/oauth2/src/main/java/org/apache/cxf/rs/security/oauth2/utils/OAuthUtils.java Fri Jul 13 12:10:14 2012 @@ -18,8 +18,12 @@ */ package org.apache.cxf.rs.security.oauth2.utils; +import java.security.Principal; +import java.util.ArrayList; +import java.util.Collections; import java.util.LinkedList; import java.util.List; +import java.util.Set; import java.util.UUID; import javax.ws.rs.core.MultivaluedMap; @@ -29,7 +33,10 @@ import org.apache.cxf.jaxrs.impl.Metadat import org.apache.cxf.jaxrs.model.URITemplate; import org.apache.cxf.rs.security.oauth2.common.Client; import org.apache.cxf.rs.security.oauth2.common.OAuthPermission; +import org.apache.cxf.rs.security.oauth2.common.UserSubject; import org.apache.cxf.rs.security.oauth2.provider.OAuthServiceException; +import org.apache.cxf.security.LoginSecurityContext; +import org.apache.cxf.security.SecurityContext; /** * Various utility methods @@ -39,6 +46,19 @@ public final class OAuthUtils { private OAuthUtils() { } + public static UserSubject createSubject(SecurityContext securityContext) { + List roleNames = Collections.emptyList(); + if (securityContext instanceof LoginSecurityContext) { + roleNames = new ArrayList(); + Set roles = ((LoginSecurityContext)securityContext).getUserRoles(); + for (Principal p : roles) { + roleNames.add(p.getName()); + } + } + return + new UserSubject(securityContext.getUserPrincipal().getName(), roleNames); + } + public static String convertPermissionsToScope(List perms) { StringBuilder sb = new StringBuilder(); for (OAuthPermission perm : perms) {