forked from eclipse-vorto/vorto
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* OAuthProvider API * fixed filter processor url * Fixed bug with logout
- Loading branch information
Showing
61 changed files
with
1,203 additions
and
865 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
71 changes: 71 additions & 0 deletions
71
.../src/main/java/org/eclipse/vorto/repository/oauth/AbstractOAuthProviderConfiguration.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,71 @@ | ||
package org.eclipse.vorto.repository.oauth; | ||
|
||
import javax.servlet.Filter; | ||
import org.eclipse.vorto.repository.web.listeners.AuthenticationSuccessHandler; | ||
import org.eclipse.vorto.repository.web.security.UserDBAuthoritiesExtractor; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.autoconfigure.security.oauth2.resource.AuthoritiesExtractor; | ||
import org.springframework.boot.autoconfigure.security.oauth2.resource.UserInfoTokenServices; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Scope; | ||
import org.springframework.security.oauth2.client.OAuth2ClientContext; | ||
import org.springframework.security.oauth2.client.OAuth2RestTemplate; | ||
import org.springframework.security.oauth2.client.filter.OAuth2ClientAuthenticationProcessingFilter; | ||
import org.springframework.security.oauth2.client.token.AccessTokenProvider; | ||
import org.springframework.security.oauth2.client.token.grant.code.AuthorizationCodeResourceDetails; | ||
|
||
public abstract class AbstractOAuthProviderConfiguration implements IOAuthFlowConfiguration { | ||
|
||
@Autowired | ||
protected AuthenticationSuccessHandler successHandler; | ||
|
||
@Autowired | ||
protected AccessTokenProvider accessTokenProvider; | ||
|
||
@Autowired | ||
protected OAuth2ClientContext oauth2ClientContext; | ||
|
||
protected UserInfoTokenServices tokenService; | ||
|
||
public AbstractOAuthProviderConfiguration(UserInfoTokenServices tokenService) { | ||
this.tokenService = tokenService; | ||
this.tokenService.setAuthoritiesExtractor(new UserDBAuthoritiesExtractor(getUserAttributeId())); | ||
} | ||
|
||
|
||
public UserInfoTokenServices getUserInfoTokenService() { | ||
return this.tokenService; | ||
} | ||
|
||
public Filter createFilter() { | ||
OAuth2RestTemplate restTemplate = createOAuthTemplate(); | ||
|
||
restTemplate.setAccessTokenProvider(accessTokenProvider); | ||
|
||
OAuth2ClientAuthenticationProcessingFilter filter = | ||
new OAuth2ClientAuthenticationProcessingFilter("/"+getFilterProcessingUrl()); | ||
filter.setAuthenticationSuccessHandler(successHandler); | ||
tokenService.setRestTemplate(restTemplate); | ||
tokenService.setAuthoritiesExtractor(authoritiesExtractor(getUserAttributeId())); | ||
filter.setRestTemplate(restTemplate); | ||
filter.setTokenServices(tokenService); | ||
|
||
return filter; | ||
} | ||
|
||
protected OAuth2RestTemplate createOAuthTemplate() { | ||
return new OAuth2RestTemplate(createDetails(), oauth2ClientContext); | ||
} | ||
|
||
protected abstract AuthorizationCodeResourceDetails createDetails(); | ||
|
||
public abstract String getFilterProcessingUrl(); | ||
|
||
protected abstract String getUserAttributeId(); | ||
|
||
@Bean | ||
@Scope("prototype") | ||
public AuthoritiesExtractor authoritiesExtractor(String userAttributeId) { | ||
return new UserDBAuthoritiesExtractor(userAttributeId); | ||
} | ||
} |
57 changes: 57 additions & 0 deletions
57
...sitory-core/src/main/java/org/eclipse/vorto/repository/oauth/IOAuthFlowConfiguration.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,57 @@ | ||
/** | ||
* Copyright (c) 2018 Contributors to the Eclipse Foundation | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information regarding copyright ownership. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* https://www.eclipse.org/legal/epl-2.0 | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package org.eclipse.vorto.repository.oauth; | ||
|
||
import javax.servlet.Filter; | ||
import javax.servlet.http.HttpServletRequest; | ||
import org.springframework.boot.autoconfigure.security.oauth2.resource.UserInfoTokenServices; | ||
|
||
/** | ||
* OAuth2 Webflow Configuration | ||
* | ||
* @author Alexander Edelmann (Robert Bosch (SEA) Pte. Ltd) | ||
* | ||
*/ | ||
public interface IOAuthFlowConfiguration { | ||
|
||
/** | ||
* Creates the filter that activates this oauth provider | ||
* @return | ||
*/ | ||
Filter createFilter(); | ||
|
||
/** | ||
* Returns the user token information service for this provider | ||
* @return | ||
*/ | ||
UserInfoTokenServices getUserInfoTokenService(); | ||
|
||
/** | ||
* Gets the logout Url for this oauth provider | ||
* @param request | ||
* @return | ||
*/ | ||
String getLogoutUrl(HttpServletRequest request); | ||
|
||
/** | ||
* Gets the oauth provider specific logo url | ||
* @return | ||
*/ | ||
String getLogoHref(); | ||
|
||
/** | ||
* Gets the url to initiate the oauth web flow | ||
* @return | ||
*/ | ||
String getFilterProcessingUrl(); | ||
} |
102 changes: 102 additions & 0 deletions
102
...tory/repository-core/src/main/java/org/eclipse/vorto/repository/oauth/IOAuthProvider.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,102 @@ | ||
/** | ||
* Copyright (c) 2018 Contributors to the Eclipse Foundation | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information regarding copyright ownership. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* https://www.eclipse.org/legal/epl-2.0 | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package org.eclipse.vorto.repository.oauth; | ||
|
||
import java.util.Optional; | ||
import javax.servlet.http.HttpServletRequest; | ||
import org.springframework.security.core.Authentication; | ||
|
||
/** | ||
* | ||
* @author Alexander Edelmann (Robert Bosch (SEA) Pte. Ltd) | ||
* | ||
*/ | ||
public interface IOAuthProvider { | ||
|
||
/** | ||
* A unique ID of this provider | ||
* @return | ||
*/ | ||
String getId(); | ||
|
||
/** | ||
* A descriptive label for the OAuth Provider | ||
* @return | ||
*/ | ||
String getLabel(); | ||
|
||
/** | ||
* Checks if the provider can handle the given authentication object | ||
* @param auth | ||
* @return | ||
*/ | ||
boolean canHandle(Authentication authentication); | ||
|
||
/** | ||
* | ||
* Checks if this provider can handle the given token | ||
* @param jwtToken | ||
* @return | ||
*/ | ||
boolean canHandle(String jwtToken); | ||
|
||
/** | ||
* Performs the actual authentication for the given token | ||
* @param request http servlet request | ||
* @param jwtToken JSON Webtoken to authenticate | ||
* @return Authentication object if the provided token is valid | ||
* | ||
* @throws OAuthAuthenticationException if the specific token request cannot be verified | ||
*/ | ||
Authentication authenticate(HttpServletRequest request, String jwtToken) throws OAuthAuthenticationException; | ||
|
||
|
||
/** | ||
* Creates an OAuth user for the given authentication object | ||
* | ||
* @param authentication | ||
* @return OAuthUser containing information about the authenticated user | ||
*/ | ||
OAuthUser createUser(Authentication authentication); | ||
|
||
/** | ||
* Indicates if the provider supports OAuth Webflow. If yes, a {@link IOAuthProvider#getWebflowConfiguration()}} can be read | ||
* @return true if the provider supports web flow , false otherwise | ||
*/ | ||
boolean supportsWebflow(); | ||
|
||
/** | ||
* Returns a webflow configuration for the oauth provider | ||
* @return | ||
*/ | ||
Optional<IOAuthFlowConfiguration> getWebflowConfiguration(); | ||
|
||
|
||
public class OAuthAuthenticationException extends Exception { | ||
|
||
/** | ||
* | ||
*/ | ||
private static final long serialVersionUID = 1L; | ||
public OAuthAuthenticationException(Throwable t) { | ||
super(t); | ||
} | ||
|
||
public OAuthAuthenticationException(String msg) { | ||
super(msg); | ||
} | ||
public OAuthAuthenticationException(String msg, Throwable t) { | ||
super(msg,t); | ||
} | ||
} | ||
} |
Oops, something went wrong.