Skip to content

Commit

Permalink
Merge pull request #57 from SmartThingsOSS/guice
Browse files Browse the repository at this point in the history
Add Guice bindings for RequireAuthHandler and BearerTokenAuthHandler
  • Loading branch information
mrobinet authored Oct 11, 2022
2 parents 6ea1ebf + a116765 commit 0d2dbe5
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,22 +1,29 @@
package st.ratpack.auth.springsec;

import com.google.inject.*;
import com.google.inject.Inject;
import com.google.inject.Provider;
import com.google.inject.Provides;
import com.google.inject.Scopes;
import com.google.inject.multibindings.OptionalBinder;
import ratpack.guice.ConfigurableModule;
import ratpack.http.client.HttpClient;
import st.ratpack.auth.*;
import st.ratpack.auth.CachingTokenValidator;
import st.ratpack.auth.TokenProvider;
import st.ratpack.auth.TokenValidator;
import st.ratpack.auth.handler.BearerTokenAuthHandler;
import st.ratpack.auth.handler.RequireAuthHandler;

import java.net.URI;

public class SpringSecCheckAuthModule extends ConfigurableModule<SpringSecCheckAuthModule.Config> {

@Override
protected void configure() {
bind(RequireAuthHandler.class).in(Scopes.SINGLETON);
OptionalBinder.newOptionalBinder(binder(), TokenProvider.class)
.setDefault().to(SpringSecCheckTokenProvider.class).in(Scopes.SINGLETON);
.setDefault().to(SpringSecCheckTokenProvider.class).in(Scopes.SINGLETON);

OptionalBinder.newOptionalBinder(binder(), TokenValidator.class)
.setDefault().toProvider(TokenValidatorProvider.class).in(Scopes.SINGLETON);
.setDefault().toProvider(TokenValidatorProvider.class).in(Scopes.SINGLETON);
}

public static class Config {
Expand Down Expand Up @@ -55,7 +62,6 @@ public void setConnectRetries(int retries) {
}



public void setUser(String user) {
this.user = user;
}
Expand Down Expand Up @@ -84,4 +90,8 @@ public TokenValidator get() {
}
}

@Provides
public BearerTokenAuthHandler defaultBearerTokenAuthHandler(TokenValidator validator) {
return new BearerTokenAuthHandler(validator);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ package st.ratpack.auth.handler

import io.netty.handler.codec.http.HttpHeaderNames
import ratpack.exec.Promise
import ratpack.handling.UserId
import spock.lang.Specification
import spock.lang.Unroll
import st.ratpack.auth.DefaultValidateTokenResult
import st.ratpack.auth.User
import st.ratpack.auth.ValidateTokenResult
import st.ratpack.auth.internal.DefaultOAuthToken
import st.ratpack.auth.OAuthToken
Expand All @@ -17,7 +19,10 @@ class BearerTokenAuthHandlerSpec extends Specification {
def "Should set OAuthToken on valid token"() {
given:
def tokenValidator = Mock(TokenValidator)
def oAuthToken = new DefaultOAuthToken.Builder().build()
def oAuthToken = new DefaultOAuthToken.Builder().setAdditionalInformation([
"user_name": "[email protected]",
"uuid" : UUID.randomUUID().toString()
]) build()

when:
def result = handle(new BearerTokenAuthHandler(tokenValidator)) {
Expand All @@ -28,6 +33,8 @@ class BearerTokenAuthHandlerSpec extends Specification {
1 * tokenValidator.validate("Token") >> Promise.value(ValidateTokenResult.valid(oAuthToken))
result.getRegistry().maybeGet(OAuthToken).present
result.getRegistry().maybeGet(ValidateTokenResult).present
result.getRegistry().maybeGet(User).present
result.getRegistry().maybeGet(UserId).present
result.calledNext
}

Expand All @@ -45,9 +52,9 @@ class BearerTokenAuthHandlerSpec extends Specification {

where:
authHeader << [
"",
"Basic BLAH",
"Bearer Token Something"
"",
"Basic BLAH",
"Bearer Token Something"
]
}

Expand Down

0 comments on commit 0d2dbe5

Please sign in to comment.