-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
1,897 additions
and
3 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
#!/bin/bash | ||
# | ||
# Script to install Keycloak on a CI server. | ||
# | ||
|
||
set -u # Fail on unset variables | ||
set -e # Fail if any command fails | ||
|
||
DIST="keycloak-1.2.1.Smartling-SNAPSHOT" | ||
ARCHIVE="${DIST}.tar.gz" | ||
|
||
if [ ! -e ${DIST} ]; then | ||
wget https://s3.amazonaws.com/keycloak-server/${ARCHIVE} | ||
tar xzf ${ARCHIVE} | ||
fi | ||
|
||
cp spring-demo-realm.json /tmp | ||
|
||
cd ${DIST} | ||
(./bin/standalone.sh -Dkeycloak.migration.action=import \ | ||
-Dkeycloak.migration.provider=singleFile \ | ||
-Dkeycloak.migration.file=/tmp/spring-demo-realm.json \ | ||
-Dkeycloak.migration.strategy=OVERWRITE_EXISTING 2>&1 > /tmp/keyloak.log &) & | ||
|
138 changes: 138 additions & 0 deletions
138
...cloak/adapters/springsecurity/authentication/DirectAccessGrantAuthenticationProvider.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,138 @@ | ||
/* | ||
* Copyright 2015 Smartling, Inc. | ||
* | ||
* 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 org.keycloak.adapters.springsecurity.authentication; | ||
|
||
import org.keycloak.KeycloakPrincipal; | ||
import org.keycloak.VerificationException; | ||
import org.keycloak.adapters.AdapterUtils; | ||
import org.keycloak.adapters.KeycloakAccount; | ||
import org.keycloak.adapters.RefreshableKeycloakSecurityContext; | ||
import org.keycloak.adapters.springsecurity.AdapterDeploymentContextBean; | ||
import org.keycloak.adapters.springsecurity.account.KeycloakRole; | ||
import org.keycloak.adapters.springsecurity.account.SimpleKeycloakAccount; | ||
import org.keycloak.adapters.springsecurity.service.DirectAccessGrantService; | ||
import org.keycloak.adapters.springsecurity.token.DirectAccessGrantToken; | ||
import org.keycloak.adapters.springsecurity.token.KeycloakAuthenticationToken; | ||
import org.springframework.beans.factory.annotation.Required; | ||
import org.springframework.security.authentication.AuthenticationProvider; | ||
import org.springframework.security.authentication.AuthenticationServiceException; | ||
import org.springframework.security.authentication.BadCredentialsException; | ||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.core.AuthenticationException; | ||
import org.springframework.security.core.GrantedAuthority; | ||
import org.springframework.security.core.authority.mapping.GrantedAuthoritiesMapper; | ||
import org.springframework.util.Assert; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Set; | ||
|
||
/** | ||
* {@link AuthenticationProvider} implementing the OAuth2 resource owner password credentials | ||
* grant for clients secured by Keycloak. | ||
* | ||
* <p> | ||
* The resource owner password credentials grant type is suitable in | ||
* cases where the resource owner has a trust relationship with the | ||
* client, such as the device operating system or a highly privileged | ||
* application. | ||
* </p> | ||
* | ||
* @author <a href="mailto:[email protected]">Scott Rossillo</a> | ||
*/ | ||
public class DirectAccessGrantAuthenticationProvider implements AuthenticationProvider { | ||
|
||
private AdapterDeploymentContextBean adapterDeploymentContextBean; | ||
private DirectAccessGrantService directAccessGrantService; | ||
private GrantedAuthoritiesMapper grantedAuthoritiesMapper = null; | ||
|
||
@Override | ||
public Authentication authenticate(Authentication authentication) throws AuthenticationException { | ||
String username = (String) authentication.getPrincipal(); | ||
String password = (String) authentication.getCredentials(); | ||
RefreshableKeycloakSecurityContext context; | ||
KeycloakAuthenticationToken token; | ||
Collection<? extends GrantedAuthority> authorities; | ||
|
||
try { | ||
context = directAccessGrantService.login(username, password); | ||
authorities = this.createGrantedAuthorities(context); | ||
token = new KeycloakAuthenticationToken(createAccount(context), authorities); | ||
} catch (VerificationException e) { | ||
throw new BadCredentialsException("Unable to validate token", e); | ||
} catch (Exception e) { | ||
throw new AuthenticationServiceException("Error authenticating with Keycloak server", e); | ||
} | ||
|
||
return token; | ||
} | ||
|
||
private KeycloakAccount createAccount(RefreshableKeycloakSecurityContext context) { | ||
Assert.notNull(context); | ||
Set<String> roles = AdapterUtils.getRolesFromSecurityContext(context); | ||
KeycloakPrincipal<RefreshableKeycloakSecurityContext> principal = | ||
AdapterUtils.createPrincipal(adapterDeploymentContextBean.getDeployment(), context); | ||
|
||
return new SimpleKeycloakAccount(principal, roles, context); | ||
} | ||
|
||
private Collection<? extends GrantedAuthority> createGrantedAuthorities(RefreshableKeycloakSecurityContext context) { | ||
List<KeycloakRole> grantedAuthorities = new ArrayList<>(); | ||
|
||
for (String role : AdapterUtils.getRolesFromSecurityContext(context)) { | ||
grantedAuthorities.add(new KeycloakRole(role)); | ||
} | ||
|
||
return mapAuthorities(Collections.unmodifiableList(grantedAuthorities)); | ||
} | ||
|
||
private Collection<? extends GrantedAuthority> mapAuthorities(Collection<? extends GrantedAuthority> authorities) { | ||
if (grantedAuthoritiesMapper == null) { | ||
return authorities; | ||
} | ||
return grantedAuthoritiesMapper.mapAuthorities(authorities); | ||
} | ||
|
||
|
||
@Override | ||
public boolean supports(Class<?> authentication) { | ||
return DirectAccessGrantToken.class.isAssignableFrom(authentication) | ||
|| UsernamePasswordAuthenticationToken.class.isAssignableFrom(authentication); | ||
} | ||
|
||
@Required | ||
public void setAdapterDeploymentContextBean(AdapterDeploymentContextBean adapterDeploymentContextBean) { | ||
this.adapterDeploymentContextBean = adapterDeploymentContextBean; | ||
} | ||
|
||
@Required | ||
public void setDirectAccessGrantService(DirectAccessGrantService directAccessGrantService) { | ||
this.directAccessGrantService = directAccessGrantService; | ||
} | ||
|
||
/** | ||
* Set the optional {@link GrantedAuthoritiesMapper} for this {@link AuthenticationProvider}. | ||
* | ||
* @param grantedAuthoritiesMapper the <code>GrantedAuthoritiesMapper</code> to use | ||
*/ | ||
public void setGrantedAuthoritiesMapper(GrantedAuthoritiesMapper grantedAuthoritiesMapper) { | ||
this.grantedAuthoritiesMapper = grantedAuthoritiesMapper; | ||
} | ||
} |
103 changes: 103 additions & 0 deletions
103
...ers/springsecurity/authentication/DirectAccessGrantUserDetailsAuthenticationProvider.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,103 @@ | ||
/* | ||
* Copyright 2015 Smartling, Inc. | ||
* | ||
* 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 org.keycloak.adapters.springsecurity.authentication; | ||
|
||
import org.keycloak.KeycloakPrincipal; | ||
import org.keycloak.adapters.KeycloakAccount; | ||
import org.keycloak.adapters.springsecurity.token.KeycloakAuthenticationToken; | ||
import org.keycloak.adapters.springsecurity.token.KeycloakUserDetailsAuthenticationToken; | ||
import org.springframework.beans.factory.annotation.Required; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.core.AuthenticationException; | ||
import org.springframework.security.core.userdetails.UserDetails; | ||
import org.springframework.security.core.userdetails.UserDetailsService; | ||
import org.springframework.util.Assert; | ||
|
||
import java.security.Principal; | ||
|
||
/** | ||
* Provides a {@link DirectAccessGrantAuthenticationProvider} capable of | ||
* swapping the Keycloak principal with a {@link UserDetails user details} principal. | ||
* | ||
* <p> | ||
* The supplied {@link UserDetailsService} is consulted using the Keycloak | ||
* access token's principal attribute as the username. | ||
* </p> | ||
* <p> | ||
* The original Keycloak principal is available from the {@link KeycloakAuthenticationToken}: | ||
* <pre> | ||
* KeycloakAuthenticationToken token = (KeycloakAuthenticationToken) SecurityContextHolder.getContext().getAuthentication()); | ||
* KeycloakAccount account = token.getAccount(); | ||
* Principal = account.getPrincipal(); | ||
* </pre> | ||
* </p> | ||
* | ||
* @author <a href="mailto:[email protected]">Scott Rossillo</a> | ||
* | ||
* @see UserDetailsService#loadUserByUsername | ||
* @see KeycloakUserDetailsAuthenticationToken | ||
*/ | ||
public class DirectAccessGrantUserDetailsAuthenticationProvider extends DirectAccessGrantAuthenticationProvider { | ||
|
||
private UserDetailsService userDetailsService; | ||
|
||
@Override | ||
public Authentication authenticate(Authentication authentication) throws AuthenticationException { | ||
|
||
KeycloakAuthenticationToken token = (KeycloakAuthenticationToken) super.authenticate(authentication); | ||
String username; | ||
UserDetails userDetails; | ||
|
||
if (token == null) { | ||
return null; | ||
} | ||
|
||
username = this.resolveUsername(token); | ||
userDetails = userDetailsService.loadUserByUsername(username); | ||
|
||
return new KeycloakUserDetailsAuthenticationToken(userDetails, token.getAccount(), token.getAuthorities()); | ||
} | ||
|
||
/** | ||
* Returns the username from the given {@link KeycloakAuthenticationToken}. By default, this method | ||
* resolves the username from the token's {@link KeycloakPrincipal}'s name. This value can be controlled | ||
* via <code>keycloak.json</code>'s | ||
* <a href="http://docs.jboss.org/keycloak/docs/1.2.0.CR1/userguide/html/ch08.html#adapter-config"><code>principal-attribute</code></a>. | ||
* For more fine-grained username resolution, override this method. | ||
* | ||
* @param token the {@link KeycloakAuthenticationToken} from which to extract the username | ||
* | ||
* @return the username to use when loading a user from the this provider's {@link UserDetailsService}. | ||
* | ||
* @see UserDetailsService#loadUserByUsername | ||
* @see KeycloakAccount#getPrincipal | ||
*/ | ||
protected String resolveUsername(KeycloakAuthenticationToken token) { | ||
|
||
Assert.notNull(token, "KeycloakAuthenticationToken required"); | ||
Assert.notNull(token.getAccount(), "KeycloakAuthenticationToken.getAccount() cannot be return null"); | ||
KeycloakAccount account = token.getAccount(); | ||
Principal principal = account.getPrincipal(); | ||
|
||
return principal.getName(); | ||
} | ||
|
||
@Required | ||
public void setUserDetailsService(UserDetailsService userDetailsService) { | ||
this.userDetailsService = userDetailsService; | ||
} | ||
} |
Oops, something went wrong.