Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make authentication dynamically configurable #28

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.eclipsesource.restfuse.example;

import static com.eclipsesource.restfuse.Assert.assertUnauthorized;
import static com.eclipsesource.restfuse.AuthenticationType.BASIC;

import org.junit.Rule;
import org.junit.runner.RunWith;

import com.eclipsesource.restfuse.Destination;
import com.eclipsesource.restfuse.HttpJUnitRunner;
import com.eclipsesource.restfuse.Method;
import com.eclipsesource.restfuse.Response;
import com.eclipsesource.restfuse.annotation.Context;
import com.eclipsesource.restfuse.annotation.HttpTest;

@RunWith(HttpJUnitRunner.class)
public class DynamicAuthenticationTest {
@Rule
public Destination restfuse = new Destination(this, "https://eclipsesource.com");

@Context
private Response response;

@HttpTest(method = Method.GET, path = "/blogs/wp_admin")
public void testAuthentication() {
assertUnauthorized(response);
}

@HttpTest(method = Method.GET, path = "/blogs/wp_admin")
public void testAuthenticationWithInvalidCredentials() {
restfuse.getRequestContext().setAuthentication("invalid", "invalid", BASIC);
assertUnauthorized(response);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.util.Map;

import com.eclipsesource.restfuse.annotation.Header;
import com.eclipsesource.restfuse.internal.AuthenticationInfo;

/**
* <p>
Expand All @@ -39,6 +40,8 @@ public class RequestContext {

private Map<String, String> dynamicPathSegments = new HashMap<String, String>();

private AuthenticationInfo authentication = null;

/**
* <p>
* Adds a header attribute to a request.
Expand Down Expand Up @@ -80,4 +83,32 @@ public Map<String, String> getPathSegments() {
return new HashMap<String, String>( dynamicPathSegments );
}

/**
* Sets the authentication information for this {@link RequestContext} using {@link AuthenticationType#BASIC}.
* @param user - user name
* @param password - password
* @return {@link RequestContext}
*/
public RequestContext setAuthentication(String user, String password){
return setAuthentication( user, password , AuthenticationType.BASIC );
}

/**
* Sets the authentication information for this {@link RequestContext}.
* @param user - user name
* @param password - password
* @return {@link RequestContext}
*/
public RequestContext setAuthentication(String user, String password, AuthenticationType type){
authentication = new AuthenticationInfo( type, user, password );
return this;
}

/**
* @return {@link AuthenticationInfo}
*/
public AuthenticationInfo getAuthentication(){
return authentication;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors: Holger Staudacher - initial API and implementation
*
******************************************************************************/
package com.eclipsesource.restfuse.internal;

Expand Down Expand Up @@ -43,7 +42,7 @@ public InternalRequest createRequest( RequestContext context ) {
HttpTest call = description.getAnnotation( HttpTest.class );
String rawPath = combineUrlAndPath( baseUrl, call.path() );
InternalRequest request = new InternalRequest( substituePathSegments( rawPath, context ) );
addAuthentication( call, request );
addAuthentication( call, request, context );
addContentType( call, request );
addHeader( call, request, context );
addBody( call, request );
Expand All @@ -57,19 +56,26 @@ private String substituePathSegments( String path, RequestContext context ) {
while( matcher.find() ) {
String segment = matcher.group( 1 );
checkSubstitutionExists( context, segment );
substitutedPath = substitutedPath.replace( "{" + segment + "}",
context.getPathSegments().get( segment ) );
substitutedPath = substitutedPath.replace( "{" + segment + "}", context.getPathSegments()
.get( segment ) );
}
return substitutedPath;
}

private void checkSubstitutionExists( RequestContext context, String segment ) {
if( !context.getPathSegments().containsKey( segment ) ) {
throw new IllegalStateException( "Misconfigured Destination. Could not replace {" + segment + "}." );
throw new IllegalStateException( "Misconfigured Destination. Could not replace {"
+ segment
+ "}." );
}
}

private void addAuthentication( HttpTest call, InternalRequest request ) {
private void addAuthentication( HttpTest call, InternalRequest request, RequestContext context ) {
addAuthenticationFromContext( request, context );
addAuthenticationFromAnnotation( call, request );
}

private void addAuthenticationFromAnnotation( HttpTest call, InternalRequest request ) {
Authentication[] authentications = call.authentications();
if( authentications != null ) {
for( Authentication authentication : authentications ) {
Expand All @@ -81,6 +87,13 @@ private void addAuthentication( HttpTest call, InternalRequest request ) {
}
}

private void addAuthenticationFromContext( InternalRequest request, RequestContext context ) {
AuthenticationInfo authentication = context.getAuthentication();
if( authentication != null ) {
request.addAuthenticationInfo( authentication );
}
}

private void addContentType( HttpTest call, InternalRequest request ) {
MediaType contentType = call.type();
if( contentType != null ) {
Expand Down