-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
More tests for JWTAuthenticationProvider
- Loading branch information
1 parent
fc7148d
commit cd8ef61
Showing
9 changed files
with
683 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
23 changes: 23 additions & 0 deletions
23
iam-login-service/src/main/java/it/infn/mw/iam/core/client/ClientUserDetailsService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/** | ||
* Copyright (c) Istituto Nazionale di Fisica Nucleare (INFN). 2016-2019 | ||
* | ||
* 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 | ||
* | ||
* 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 it.infn.mw.iam.core.client; | ||
|
||
import org.mitre.oauth2.service.ClientDetailsEntityService; | ||
import org.springframework.security.core.userdetails.UserDetailsService; | ||
|
||
public interface ClientUserDetailsService extends UserDetailsService { | ||
ClientDetailsEntityService getClientDetailsService(); | ||
} |
78 changes: 78 additions & 0 deletions
78
iam-login-service/src/main/java/it/infn/mw/iam/core/client/IAMClientUserDetailsService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
/** | ||
* Copyright (c) Istituto Nazionale di Fisica Nucleare (INFN). 2016-2019 | ||
* | ||
* 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 | ||
* | ||
* 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 it.infn.mw.iam.core.client; | ||
|
||
import java.util.Collection; | ||
import java.util.Optional; | ||
import java.util.function.Supplier; | ||
|
||
import org.mitre.oauth2.model.ClientDetailsEntity; | ||
import org.mitre.oauth2.service.ClientDetailsEntityService; | ||
import org.springframework.security.core.GrantedAuthority; | ||
import org.springframework.security.core.authority.SimpleGrantedAuthority; | ||
import org.springframework.security.core.userdetails.User; | ||
import org.springframework.security.core.userdetails.UserDetails; | ||
import org.springframework.security.core.userdetails.UsernameNotFoundException; | ||
import org.springframework.security.oauth2.common.exceptions.InvalidClientException; | ||
|
||
import com.google.common.base.Strings; | ||
import com.google.common.collect.Sets; | ||
|
||
public class IAMClientUserDetailsService implements ClientUserDetailsService { | ||
private static GrantedAuthority ROLE_CLIENT = new SimpleGrantedAuthority("ROLE_CLIENT"); | ||
|
||
private final ClientDetailsEntityService clientService; | ||
|
||
public IAMClientUserDetailsService(ClientDetailsEntityService clientService) { | ||
this.clientService = clientService; | ||
} | ||
|
||
private Supplier<UsernameNotFoundException> unknownClientError(String clientId) { | ||
return () -> new UsernameNotFoundException("Unknown client: " + clientId); | ||
} | ||
|
||
@Override | ||
public UserDetails loadUserByUsername(String clientId) throws UsernameNotFoundException { | ||
|
||
try { | ||
ClientDetailsEntity client = Optional.ofNullable(clientService.loadClientByClientId(clientId)) | ||
.orElseThrow(unknownClientError(clientId)); | ||
|
||
final String password = Strings.nullToEmpty(client.getClientSecret()); | ||
|
||
final boolean accountEnabled = true; | ||
final boolean accountNonExpired = true; | ||
final boolean credentialsNonExpired = true; | ||
final boolean accountNonLocked = true; | ||
|
||
Collection<GrantedAuthority> authorities = Sets.newHashSet(client.getAuthorities()); | ||
authorities.add(ROLE_CLIENT); | ||
|
||
return new User(clientId, password, accountEnabled, accountNonExpired, credentialsNonExpired, | ||
accountNonLocked, authorities); | ||
|
||
} catch (InvalidClientException e) { | ||
throw unknownClientError(clientId).get(); | ||
} | ||
} | ||
|
||
@Override | ||
public ClientDetailsEntityService getClientDetailsService() { | ||
|
||
return clientService; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
73 changes: 73 additions & 0 deletions
73
...va/it/infn/mw/iam/test/oauth/assertion/IAMJWTBearerAuthenticationProviderTestSupport.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
/** | ||
* Copyright (c) Istituto Nazionale di Fisica Nucleare (INFN). 2016-2019 | ||
* | ||
* 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 | ||
* | ||
* 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 it.infn.mw.iam.test.oauth.assertion; | ||
|
||
import static org.mockito.Mockito.when; | ||
|
||
import java.util.function.Consumer; | ||
|
||
import org.mitre.oauth2.model.ClientDetailsEntity; | ||
import org.mitre.oauth2.model.ClientDetailsEntity.AuthMethod; | ||
import org.springframework.security.core.GrantedAuthority; | ||
import org.springframework.security.core.authority.SimpleGrantedAuthority; | ||
|
||
import com.nimbusds.jose.JOSEException; | ||
import com.nimbusds.jose.JWSAlgorithm; | ||
import com.nimbusds.jose.JWSHeader; | ||
import com.nimbusds.jose.crypto.MACSigner; | ||
import com.nimbusds.jwt.JWTClaimsSet; | ||
import com.nimbusds.jwt.SignedJWT; | ||
|
||
public interface IAMJWTBearerAuthenticationProviderTestSupport { | ||
|
||
String JWT_AUTH_NAME = "jwt-bearer-client"; | ||
String ISSUER = "http://localhost:8080/"; | ||
String ISSUER_NO_TRAILING_SLASH = "http://localhost:8080/"; | ||
|
||
String ISSUER_TOKEN_ENDPOINT = "http://localhost:8080/token"; | ||
|
||
String CLIENT_SECRET = "bf4a39e1-43df-4e6f-b9b8-9a359108ac91"; | ||
|
||
JWSHeader JWS_HEADER_HS256 = new JWSHeader(JWSAlgorithm.HS256); | ||
|
||
JWTClaimsSet JUST_SUB_JWT = new JWTClaimsSet.Builder().subject("jwt-bearer-client").build(); | ||
|
||
|
||
GrantedAuthority ROLE_CLIENT_AUTHORITY = new SimpleGrantedAuthority("ROLE_CLIENT"); | ||
|
||
default SignedJWT macSignJwt(JWTClaimsSet claimSet) throws JOSEException { | ||
|
||
SignedJWT jws = new SignedJWT(JWS_HEADER_HS256, claimSet); | ||
MACSigner signer = new MACSigner(CLIENT_SECRET); | ||
|
||
jws.sign(signer); | ||
return jws; | ||
|
||
} | ||
|
||
default void testForAllAlgos(ClientDetailsEntity client, | ||
Consumer<JWSAlgorithm> test) { | ||
|
||
when(client.getTokenEndpointAuthMethod()).thenReturn(AuthMethod.SECRET_JWT); | ||
JWSAlgorithm.Family.HMAC_SHA.forEach(test); | ||
when(client.getTokenEndpointAuthMethod()).thenReturn(AuthMethod.PRIVATE_KEY); | ||
JWSAlgorithm.Family.SIGNATURE.forEach(test); | ||
} | ||
|
||
|
||
|
||
|
||
} |
Oops, something went wrong.