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

Commit

Permalink
DefaultOAuth2RequestAuthenticator resolves Bearer syntax
Browse files Browse the repository at this point in the history
Fixes gh-1346
  • Loading branch information
jgrandja committed May 7, 2018
1 parent 467ef03 commit 360d56b
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2013-2014 the original author or authors.
* Copyright 2013-2018 the original author or authors.
*
* Licensed 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
Expand Down Expand Up @@ -35,6 +35,9 @@ public void authenticate(OAuth2ProtectedResourceDetails resource, OAuth2ClientCo
String tokenType = accessToken.getTokenType();
if (!StringUtils.hasText(tokenType)) {
tokenType = OAuth2AccessToken.BEARER_TYPE; // we'll assume basic bearer token type if none is specified.
} else if (tokenType.equalsIgnoreCase(OAuth2AccessToken.BEARER_TYPE)) {
// gh-1346
tokenType = OAuth2AccessToken.BEARER_TYPE; // Ensure we use the correct syntax for the "Bearer" authentication scheme
}
request.getHeaders().set("Authorization", String.format("%s %s", tokenType, accessToken.getValue()));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2013-2014 the original author or authors.
* Copyright 2013-2018 the original author or authors.
*
* Licensed 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
Expand All @@ -13,13 +13,14 @@

package org.springframework.security.oauth2.client;

import static org.junit.Assert.assertEquals;

import org.junit.Test;
import org.springframework.mock.http.client.MockClientHttpRequest;
import org.springframework.security.oauth2.client.http.AccessTokenRequiredException;
import org.springframework.security.oauth2.client.resource.BaseOAuth2ProtectedResourceDetails;
import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
import org.springframework.security.oauth2.common.OAuth2AccessToken;

import static org.junit.Assert.assertEquals;

/**
* @author Dave Syer
Expand All @@ -45,7 +46,54 @@ public void addsAccessToken() {
BaseOAuth2ProtectedResourceDetails resource = new BaseOAuth2ProtectedResourceDetails();
authenticator.authenticate(resource, context, request);
String header = request.getHeaders().getFirst("Authorization");
assertEquals("bearer FOO", header);
assertEquals("Bearer FOO", header);
}

// gh-1346
@Test
public void authenticateWhenTokenTypeBearerUppercaseThenUseBearer() {
DefaultOAuth2AccessToken accessToken = new DefaultOAuth2AccessToken("FOO");
accessToken.setTokenType(OAuth2AccessToken.BEARER_TYPE.toUpperCase());
context.setAccessToken(accessToken);
BaseOAuth2ProtectedResourceDetails resource = new BaseOAuth2ProtectedResourceDetails();
authenticator.authenticate(resource, context, request);
String header = request.getHeaders().getFirst("Authorization");
assertEquals("Bearer FOO", header);
}

}
// gh-1346
@Test
public void authenticateWhenTokenTypeBearerLowercaseThenUseBearer() {
DefaultOAuth2AccessToken accessToken = new DefaultOAuth2AccessToken("FOO");
accessToken.setTokenType(OAuth2AccessToken.BEARER_TYPE.toLowerCase());
context.setAccessToken(accessToken);
BaseOAuth2ProtectedResourceDetails resource = new BaseOAuth2ProtectedResourceDetails();
authenticator.authenticate(resource, context, request);
String header = request.getHeaders().getFirst("Authorization");
assertEquals("Bearer FOO", header);
}

// gh-1346
@Test
public void authenticateWhenTokenTypeBearerMixcaseThenUseBearer() {
DefaultOAuth2AccessToken accessToken = new DefaultOAuth2AccessToken("FOO");
accessToken.setTokenType("BeaRer");
context.setAccessToken(accessToken);
BaseOAuth2ProtectedResourceDetails resource = new BaseOAuth2ProtectedResourceDetails();
authenticator.authenticate(resource, context, request);
String header = request.getHeaders().getFirst("Authorization");
assertEquals("Bearer FOO", header);
}

// gh-1346
@Test
public void authenticateWhenTokenTypeMACThenUseMAC() {
DefaultOAuth2AccessToken accessToken = new DefaultOAuth2AccessToken("FOO");
accessToken.setTokenType("MAC");
context.setAccessToken(accessToken);
BaseOAuth2ProtectedResourceDetails resource = new BaseOAuth2ProtectedResourceDetails();
authenticator.authenticate(resource, context, request);
String header = request.getHeaders().getFirst("Authorization");
assertEquals("MAC FOO", header);
}
}

0 comments on commit 360d56b

Please sign in to comment.