Commit e06513fa authored by Kateryna Kliushnyk's avatar Kateryna Kliushnyk Committed by Matija Obreza
Browse files

Tokens management finished

Conflicts:
	.gitignore
	crophub_oauth_client/pom.xml
	crophub_oauth_client/src/main/java/org/crophub/auth/client/CropHubClient.java
	crophub_server_oauth/pom.xml
	crophub_server_oauth/src/main/java/org/crophub/auth/server/controller/DataSetController.java
	crophub_server_oauth/src/main/java/org/crophub/auth/server/controller/LicensesController.java
	crophub_server_oauth/src/main/java/org/crophub/auth/server/controller/OrganizationController.java
	crophub_server_oauth/src/main/java/org/crophub/auth/server/controller/TraitsController.java
	crophub_server_oauth/src/main/java/org/crophub/auth/server/controller/UserController.java
	crophub_server_oauth/src/main/resources/database.properties
	crophub_server_oauth/src/main/resources/spring/spring-oauth.xml
	crophub_server_oauth/src/main/resources/spring/spring-security.xml
	crophub_server_oauth/src/main/webapp/login.jsp
parent 6631ac5b
.gradle
.idea
*.iml
out
......@@ -7,3 +8,4 @@ logs
/bin
/lucene
/data
build
package org.crophub.auth.server.listener;
import org.crophub.auth.server.model.impl.OAuthClientDetails;
import org.crophub.auth.server.service.impl.JdbcClientDetailsService;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class CreateClientDetailsListener implements InitializingBean {
@Autowired
private JdbcClientDetailsService clientDetailsService;
public static final String DEFAULT_CLIENT_DETAILS_ID = "CropHub";
@Override
public void afterPropertiesSet() throws Exception {
System.out.println("CREATE CLIENT DETAILS LISTENER START.");
if (!clientDetailsService.isExists(DEFAULT_CLIENT_DETAILS_ID)) {
createClientDetails();
}
System.out.println("CREATE CLIENT DETAILS LISTENER END.");
}
private void createClientDetails() {
OAuthClientDetails clientDetails = new OAuthClientDetails();
clientDetails.setClientId(DEFAULT_CLIENT_DETAILS_ID);
clientDetails.setClientSecret("0xcafebabe");
clientDetails.setScope("read,write");
clientDetails.setAuthorizedGrantTypes("authorization_code");
clientDetails.setAuthorities("ROLE_USER");
clientDetailsService.addClientDetails(clientDetails);
}
}
package org.crophub.auth.server.model.impl;
import org.crophub.auth.server.model.HibernateModel;
import javax.persistence.*;
import java.sql.Blob;
@Entity
@Table(name = "oauth_access_token")
public class OAuthAccessToken implements HibernateModel {
@Id
@Column(name = "token_id", unique = true, nullable = false)
private String tokenId;
@Lob
@Column(name = "token")
private Blob token;
@Column(name = "authentication_id")
private String authenticationId;
@Column(name = "user_name")
private String userName;
@Column(name = "client_id")
private String clientId;
@Lob
@Column(name = "authentication")
private Blob authentication;
@Column(name = "refresh_token")
private String refreshToken;
public String getTokenId() {
return tokenId;
}
public void setTokenId(String tokenId) {
this.tokenId = tokenId;
}
public Blob getToken() {
return token;
}
public void setToken(Blob token) {
this.token = token;
}
public String getAuthenticationId() {
return authenticationId;
}
public void setAuthenticationId(String authenticationId) {
this.authenticationId = authenticationId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getClientId() {
return clientId;
}
public void setClientId(String clientId) {
this.clientId = clientId;
}
public Blob getAuthentication() {
return authentication;
}
public void setAuthentication(Blob authentication) {
this.authentication = authentication;
}
public String getRefreshToken() {
return refreshToken;
}
public void setRefreshToken(String refreshToken) {
this.refreshToken = refreshToken;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof OAuthAccessToken)) return false;
OAuthAccessToken that = (OAuthAccessToken) o;
if (authentication != null ? !authentication.equals(that.authentication) : that.authentication != null)
return false;
if (authenticationId != null ? !authenticationId.equals(that.authenticationId) : that.authenticationId != null)
return false;
if (clientId != null ? !clientId.equals(that.clientId) : that.clientId != null) return false;
if (refreshToken != null ? !refreshToken.equals(that.refreshToken) : that.refreshToken != null) return false;
if (token != null ? !token.equals(that.token) : that.token != null) return false;
if (tokenId != null ? !tokenId.equals(that.tokenId) : that.tokenId != null) return false;
if (userName != null ? !userName.equals(that.userName) : that.userName != null) return false;
return true;
}
@Override
public int hashCode() {
int result = tokenId != null ? tokenId.hashCode() : 0;
result = 31 * result + (token != null ? token.hashCode() : 0);
result = 31 * result + (authenticationId != null ? authenticationId.hashCode() : 0);
result = 31 * result + (userName != null ? userName.hashCode() : 0);
result = 31 * result + (clientId != null ? clientId.hashCode() : 0);
result = 31 * result + (authentication != null ? authentication.hashCode() : 0);
result = 31 * result + (refreshToken != null ? refreshToken.hashCode() : 0);
return result;
}
}
package org.crophub.auth.server.model.impl;
import org.crophub.auth.server.model.BusinessModel;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
@Entity
@Table(name = "authorities")
public class OAuthAuthorities extends BusinessModel{
@Column(name = "username")
private String username;
@Column(name = "authority")
private String authority;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getAuthority() {
return authority;
}
public void setAuthority(String authority) {
this.authority = authority;
}
}
package org.crophub.auth.server.model.impl;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.oauth2.provider.ClientDetails;
import org.springframework.util.StringUtils;
import javax.persistence.*;
import java.util.*;
@Entity
@Table(name = "oauth_client_details")
public class OAuthClientDetails implements ClientDetails {
@Id
@Column(name = "client_id", unique = true, nullable = false)
private String clientId;
@Column(name = "client_secret")
private String clientSecret;
@Column(name="resource_ids")
private String resourceIds;
@Column(name = "scope")
private String scope;
@Column(name="authorities")
private String authorities;
@Column(name="authorized_grant_types")
private String authorizedGrantTypes;
@Column(name="web_server_redirect_uri")
private String registeredRedirectUri;
@Column(name = "access_token_validity")
private Integer accessTokenValiditySeconds;
@Column(name = "refresh_token_validity")
private Integer refreshTokenValiditySeconds;
@Column(name = "additional_information")
private String additionalInformation;
public OAuthClientDetails() {
}
public OAuthClientDetails(String clientId, String resourceIds, String scopes, String grantTypes, String authorities) {
this(clientId, resourceIds, scopes, grantTypes, authorities, null);
}
public OAuthClientDetails(String clientId, String resourceIds, String scopes, String grantTypes, String authorities,
String redirectUris) {
this.clientId = clientId;
if (StringUtils.hasText(resourceIds)) {
// Set<String> resources = StringUtils.commaDelimitedListToSet(resourceIds);
if (!resourceIds.isEmpty()) {
this.resourceIds = resourceIds;
}
}
if (StringUtils.hasText(scopes)) {
// Set<String> scopeList = StringUtils.commaDelimitedListToSet(scopes);
if (!scopes.isEmpty()) {
this.scope = scopes;
}
}
if (StringUtils.hasText(grantTypes)) {
this.authorizedGrantTypes = grantTypes;
} else {
this.authorizedGrantTypes = "authorization_code, refresh_token";
}
if (StringUtils.hasText(authorities)) {
this.authorities = authorities;
}
if (StringUtils.hasText(redirectUris)) {
this.registeredRedirectUri = redirectUris;
}
}
@Override
public String getClientId() {
return clientId;
}
public void setClientId(String clientId) {
this.clientId = clientId;
}
@Override
public Set<String> getResourceIds() {
if(resourceIds != null && !resourceIds.isEmpty()){
String[] split = resourceIds.split(",");
List<String> strings = Arrays.asList(split);
return new LinkedHashSet<String>(strings);
} else {
return Collections.<String>emptySet();
}
}
public void setResourceIds(String resourceIds) {
if (!resourceIds.isEmpty()) {
this.resourceIds = resourceIds;
}
}
@Override
public String getClientSecret() {
return clientSecret;
}
public void setClientSecret(String clientSecret) {
this.clientSecret = clientSecret;
}
@Override
public Set<String> getScope() {
if(scope != null && !scope.isEmpty()){
String[] split = scope.split(",");
List<String> strings = Arrays.asList(split);
return new LinkedHashSet<String>(strings);
} else {
return Collections.<String>emptySet();
}
}
public void setScope(String scope) {
this.scope = scope;
}
@Override
public Set<String> getAuthorizedGrantTypes() {
if(authorizedGrantTypes != null && !authorizedGrantTypes.isEmpty()){
String[] split = authorizedGrantTypes.split(",");
List<String> strings = Arrays.asList(split);
return new HashSet<String>(strings);
}else{
return Collections.<String>emptySet();
}
}
public void setAuthorizedGrantTypes(String authorizedGrantTypes) {
this.authorizedGrantTypes = authorizedGrantTypes;
}
@Override
public Collection<GrantedAuthority> getAuthorities() {
if(authorities != null && !authorities.isEmpty()){
return new ArrayList<GrantedAuthority>(AuthorityUtils.createAuthorityList(authorities));
} else {
return Collections.emptyList();
}
}
private List<String> getAuthoritiesAsStrings() {
return new ArrayList<String>(AuthorityUtils.authorityListToSet(AuthorityUtils.createAuthorityList(authorities)));
}
public void setAuthorities(String authorities) {
this.authorities = authorities;
}
@Override
public Integer getAccessTokenValiditySeconds() {
return accessTokenValiditySeconds;
}
public void setAccessTokenValiditySeconds(Integer accessTokenValiditySeconds) {
this.accessTokenValiditySeconds = accessTokenValiditySeconds;
}
@Override
public Integer getRefreshTokenValiditySeconds() {
return refreshTokenValiditySeconds;
}
public void setRefreshTokenValiditySeconds(Integer refreshTokenValiditySeconds) {
this.refreshTokenValiditySeconds = refreshTokenValiditySeconds;
}
@Override
public Set<String> getRegisteredRedirectUri() {
if(registeredRedirectUri != null && !registeredRedirectUri.isEmpty()){
String[] split = registeredRedirectUri.split(",");
List<String> strings = Arrays.asList(split);
return new LinkedHashSet<String>(strings);
}else{
return Collections.<String>emptySet();
}
}
public void setRegisteredRedirectUri(String registeredRedirectUris) {
this.registeredRedirectUri = registeredRedirectUris;
}
@Override
public Map<String, Object> getAdditionalInformation() {
if(additionalInformation != null && !additionalInformation.isEmpty()){
Map<String, Object> myMap = new HashMap<String, Object>();
String[] pairs = additionalInformation.split(",");
for (int i=0;i<pairs.length;i++) {
String pair = pairs[i];
String[] keyValue = pair.split(":");
myMap.put(keyValue[0], Integer.valueOf(keyValue[1]));
}
return myMap;
}else{
return Collections.<String, Object>emptyMap();
}
}
public void setAdditionalInformation(String additionalInformation) {
this.additionalInformation = additionalInformation;
}
@Override
@Transient
public boolean isSecretRequired() {
return this.clientSecret != null;
}
@Override
@Transient
public boolean isScoped() {
return this.scope != null && !this.scope.isEmpty();
}
}
package org.crophub.auth.server.model.impl;
import org.crophub.auth.server.model.HibernateModel;
import javax.persistence.*;
import java.sql.Blob;
@Entity
@Table(name = "oauth_client_token")
public class OAuthClientToken implements HibernateModel {
@Id
@Column(name = "token_id", unique = true, nullable = false)
private String tokenId;
@Lob
@Column(name = "token")
private Blob token;
@Column(name = "authentication_id")
private String authenticationId;
@Column(name = "user_name")
private String userName;
@Column(name = "client_id")
private String clientId;
public String getTokenId() {
return tokenId;
}
public void setTokenId(String tokenId) {
this.tokenId = tokenId;
}
public Blob getToken() {
return token;
}
public void setToken(Blob token) {
this.token = token;
}
public String getAuthenticationId() {
return authenticationId;
}
public void setAuthenticationId(String authenticationId) {
this.authenticationId = authenticationId;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getClientId() {
return clientId;
}
public void setClientId(String clientId) {
this.clientId = clientId;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof OAuthClientToken)) return false;
OAuthClientToken that = (OAuthClientToken) o;
if (authenticationId != null ? !authenticationId.equals(that.authenticationId) : that.authenticationId != null)
return false;
if (clientId != null ? !clientId.equals(that.clientId) : that.clientId != null) return false;
if (token != null ? !token.equals(that.token) : that.token != null) return false;
if (tokenId != null ? !tokenId.equals(that.tokenId) : that.tokenId != null) return false;
if (userName != null ? !userName.equals(that.userName) : that.userName != null) return false;
return true;
}
@Override
public int hashCode() {
int result = tokenId != null ? tokenId.hashCode() : 0;
result = 31 * result + (token != null ? token.hashCode() : 0);
result = 31 * result + (authenticationId != null ? authenticationId.hashCode() : 0);
result = 31 * result + (userName != null ? userName.hashCode() : 0);
result = 31 * result + (clientId != null ? clientId.hashCode() : 0);
return result;
}
}
package org.crophub.auth.server.model.impl;
import org.crophub.auth.server.model.HibernateModel;
import javax.persistence.*;
import java.sql.Blob;
@Entity
@Table(name = "oauth_code")
public class OAuthCode implements HibernateModel {
@Id
@Column(name = "code", unique = true, nullable = false)
private String code;
@Lob