Commit 7e7b8d3c authored by Matija Obreza's avatar Matija Obreza

Using appblocks-security OAuth implementation

parent 0470bb37
/**
* Copyright 2014 Global Crop Diversity Trust
*
* 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.genesys2.server.model.oauth;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import org.genesys.blocks.model.BasicModel;
@Entity
@Table(name = "oauthaccesstoken")
public class OAuthAccessToken extends BasicModel implements OAuthToken {
private static final long serialVersionUID = -424404058531521676L;
@Column(nullable = false)
private String clientId;
@Column(length = 36)
private String refreshToken;
@Column
private Date createdDate;
@Temporal(TemporalType.TIMESTAMP)
@Column
private Date expiration;
@Column(length = 100)
private String tokenType;
@Column(length = 100, nullable = false)
private String value;
@Column(length = 100)
private String scopes;
@Column(length = 500)
private String additionalInfo;
@Column(length = 64)
private String userUuid;
@Column(length = 200)
private String redirectUri;
@Column(length = 200, nullable = false, unique = true)
private String authenticationId;
public Date getCreatedDate() {
return createdDate;
}
public void setCreatedDate(Date createdDate) {
this.createdDate = createdDate;
}
public String getAuthenticationId() {
return authenticationId;
}
public void setAuthenticationId(String authenticationId) {
this.authenticationId = authenticationId;
}
public String getClientId() {
return clientId;
}
public void setClientId(String clientId) {
this.clientId = clientId;
}
public String getRefreshToken() {
return refreshToken;
}
public void setRefreshToken(String refreshToken) {
this.refreshToken = refreshToken;
}
public void setExpiration(Date expiration) {
this.expiration = expiration;
}
public Date getExpiration() {
return expiration;
}
public void setTokenType(String tokenType) {
this.tokenType = tokenType;
}
public String getTokenType() {
return tokenType;
}
public void setValue(String value) {
this.value = value;
}
public String getValue() {
return value;
}
public void setScopes(String scopes) {
this.scopes = scopes;
}
public String getScopes() {
return scopes;
}
public void setAdditionalInfo(String additionalInfo) {
this.additionalInfo = additionalInfo;
}
public String getAdditionalInfo() {
return additionalInfo;
}
public void setUserUuid(String uuid) {
this.userUuid = uuid;
}
public String getUserUuid() {
return userUuid;
}
public String getRedirectUri() {
return redirectUri;
}
public void setRedirectUri(String redirectUri) {
this.redirectUri = redirectUri;
}
}
/**
* Copyright 2014 Global Crop Diversity Trust
*
* 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.genesys2.server.model.oauth;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
import org.genesys.blocks.model.BasicModel;
@Entity
@Table(name = "authorities")
public class OAuthAuthorities extends BasicModel {
private static final long serialVersionUID = 1637383923219682635L;
@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;
}
}
/**
* Copyright 2014 Global Crop Diversity Trust
*
* 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.genesys2.server.model.oauth;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Lob;
import javax.persistence.Table;
import javax.persistence.Transient;
import org.apache.commons.lang.StringUtils;
import org.genesys.blocks.model.AuditedVersionedModel;
import org.genesys.blocks.security.model.AclAwareModel;
import org.hibernate.annotations.Type;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.oauth2.provider.ClientDetails;
@Entity
@Table(name = "oauthclient")
public class OAuthClientDetails extends AuditedVersionedModel implements ClientDetails, AclAwareModel {
private static final long serialVersionUID = 5328458631619687041L;
@Column(length = 200)
private String title;
@Lob
@Column
@Type(type = "org.hibernate.type.TextType")
private String description;
@Column(unique = true, nullable = false)
private String clientId;
@Column(length = 100)
private String clientSecret;
@Column(length = 400)
private String resourceIds;
@Column(length = 400)
private String scope;
@Column(length = 400)
private String authorities;
@Column(length = 400)
private String authorizedGrantTypes;
@Column(length = 400)
private String redirectUris;
@Column
private Integer accessTokenValiditySeconds;
@Column
private Integer refreshTokenValiditySeconds;
@Column(name = "additional_information")
private String additionalInformation;
// @Enumerated(EnumType.STRING)
// @Column(name = "clientType", length=50)
// private OAuthClientType clientType;
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.isNotBlank(resourceIds)) {
this.resourceIds = resourceIds;
}
if (StringUtils.isNotBlank(scopes)) {
this.scope = scopes;
}
if (StringUtils.isNotBlank(grantTypes)) {
this.authorizedGrantTypes = grantTypes;
} else {
this.authorizedGrantTypes = "authorization_code,refresh_token";
}
if (StringUtils.isNotBlank(authorities)) {
this.authorities = authorities;
}
if (StringUtils.isNotBlank(redirectUris)) {
this.redirectUris = redirectUris;
}
}
// public OAuthClientType getClientType() {
// return clientType;
// }
//
// public void setClientType(OAuthClientType clientType) {
// this.clientType = clientType;
// }
@Override
public String getClientId() {
return clientId;
}
public void setClientId(String clientId) {
this.clientId = clientId;
}
@Override
public Set<String> getResourceIds() {
if (resourceIds != null && !resourceIds.isEmpty()) {
final String[] split = resourceIds.split(",");
final 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()) {
final String[] split = scope.split(",");
final 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()) {
final String[] split = authorizedGrantTypes.split(",");
final 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();
}
}
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 (redirectUris != null && !redirectUris.isEmpty()) {
final String[] split = redirectUris.split(",");
final List<String> strings = Arrays.asList(split);
return new LinkedHashSet<String>(strings);
} else {
return Collections.<String> emptySet();
}
}
public void setRedirectUris(String redirectUris) {
this.redirectUris = redirectUris;
}
public String getRedirectUris() {
return redirectUris;
}
@Override
public Map<String, Object> getAdditionalInformation() {
if (StringUtils.isNotBlank(additionalInformation)) {
final Map<String, Object> myMap = new HashMap<String, Object>();
final String[] pairs = additionalInformation.split(",");
for (final String pair : pairs) {
final 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();
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
/**
* Copyright 2014 Global Crop Diversity Trust
*
* 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.genesys2.server.model.oauth;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Table;
import org.genesys.blocks.model.BasicModel;
@Entity
@Table(name = "oauthclienttoken")
public class OAuthClientToken extends BasicModel {
/**
*
*/
private static final long serialVersionUID = 3212482423454184830L;
@Column(name = "authentication_id")
private String authenticationId;
@Column(name = "user_name")
private String userName;
@Column(name = "client_id")
private String clientId;
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;
}
}
/**
* Copyright 2014 Global Crop Diversity Trust
*
* 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.