Skip to content
This repository has been archived by the owner on May 31, 2022. It is now read-only.

Commit

Permalink
SECOAUTH-347 Allow custom token granters in authorization-server name…
Browse files Browse the repository at this point in the history
…space configuration
  • Loading branch information
vkryachko authored and dsyer committed Oct 31, 2012
1 parent 0d3ed66 commit 31132e1
Show file tree
Hide file tree
Showing 5 changed files with 103 additions and 0 deletions.
1 change: 1 addition & 0 deletions spring-security-oauth2/.springBeans
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<config>src/test/resources/org/springframework/security/oauth2/config/TestResourceServerBeanDefinitionParser-context.xml</config>
<config>src/test/resources/org/springframework/security/oauth2/config/TestResourceBeanDefinitionParser-context.xml</config>
<config>src/test/resources/org/springframework/security/oauth2/config/authorization-server-vanilla.xml</config>
<config>src/test/resources/org/springframework/security/oauth2/config/authorization-server-custom-grant.xml</config>
</configs>
<configSets>
</configSets>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

package org.springframework.security.oauth2.config;

import java.util.List;

import org.springframework.beans.BeanMetadataElement;
import org.springframework.beans.factory.config.TypedStringValue;
import org.springframework.beans.factory.support.AbstractBeanDefinition;
Expand Down Expand Up @@ -171,6 +173,14 @@ protected AbstractBeanDefinition parseEndpointAndReturnFilter(Element element, P
clientPasswordTokenGranter.addConstructorArgReference(clientDetailsRef);
tokenGranters.add(clientPasswordTokenGranter.getBeanDefinition());
}
List<Element> customGrantElements = DomUtils.getChildElementsByTagName(element, "custom-grant");
for(Element customGrantElement: customGrantElements) {
if(!"true".equalsIgnoreCase(customGrantElement.getAttribute("disabled"))) {
String customGranterRef = customGrantElement.getAttribute("token-granter-ref");
parserContext.getRegistry().getBeanDefinition(customGranterRef);
tokenGranters.add(parserContext.getRegistry().getBeanDefinition(customGranterRef));
}
}
}

if (registerAuthorizationEndpoint) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,30 @@
</xs:attribute>
</xs:complexType>
</xs:element>
<xs:element name="custom-grant" minOccurs="0" maxOccurs="unbounded">
<xs:annotation>
<xs:documentation>
The configuration of your custom grant type.
</xs:documentation>
</xs:annotation>
<xs:complexType>
<xs:attribute name="disabled" type="xs:boolean">
<xs:annotation>
<xs:documentation>
Whether to disable this grant
type
</xs:documentation>
</xs:annotation>
</xs:attribute>
<xs:attribute name="token-granter-ref" type="xs:string" use="required">
<xs:annotation>
<xs:documentation>
A reference to your token granter
</xs:documentation>
</xs:annotation>
</xs:attribute>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute name="client-details-service-ref" type="xs:string">
<xs:annotation>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package org.springframework.security.oauth2.config;

import static org.junit.Assert.assertNotNull;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
import org.springframework.security.oauth2.common.OAuth2AccessToken;
import org.springframework.security.oauth2.provider.AuthorizationRequest;
import org.springframework.security.oauth2.provider.CompositeTokenGranter;
import org.springframework.security.oauth2.provider.TokenGranter;

public class TestAuthorizationServerCustomGrantParser {

private static String RESOURCE_NAME = "authorization-server-custom-grant.xml";

private ConfigurableApplicationContext context;

@Rule
public ExpectedException expected = ExpectedException.none();

public TestAuthorizationServerCustomGrantParser() {
context = new GenericXmlApplicationContext(getClass(), RESOURCE_NAME);
}

@Test
public void testCustomGrantRegistered() {
TokenGranter granter = context.getBean(CompositeTokenGranter.class);
assertNotNull("Custom grant registration failed!", granter.grant("test-grant", null));
}

public static class CustomTestTokenGranter implements TokenGranter {

public CustomTestTokenGranter() {}

public OAuth2AccessToken grant(String grantType,
AuthorizationRequest authorizationRequest) {
if (grantType.equals("test-grant")) {
return new DefaultOAuth2AccessToken("test");
}
return null;
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:oauth2="http://www.springframework.org/schema/security/oauth2"
xsi:schemaLocation="http://www.springframework.org/schema/security/oauth2 http://www.springframework.org/schema/security/spring-security-oauth2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

<bean id="tokenGranter" class="org.springframework.security.oauth2.config.TestAuthorizationServerCustomGrantParser.CustomTestTokenGranter" />

<oauth2:authorization-server client-details-service-ref="clientDetails">
<oauth2:authorization-code />
<oauth2:custom-grant token-granter-ref="tokenGranter" />
</oauth2:authorization-server>

<oauth2:client-details-service id="clientDetails">
<oauth2:client client-id="foo" authorized-grant-types="password"/>
</oauth2:client-details-service>

</beans>

0 comments on commit 31132e1

Please sign in to comment.