Commit 97c9a6c3 authored by Matija Obreza's avatar Matija Obreza

Fixed package names and missing classes

parent 1366e2c0
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.message;
import java.io.Serializable;
public class SimpleMessage implements Serializable {
private String status;
private String message;
@SuppressWarnings("unused")
private SimpleMessage() {
}
public SimpleMessage(String status, String message) {
this.status = status;
this.message = message;
}
public String getStatus() {
return status;
}
public String getMessage() {
return message;
}
@Override
public String toString() {
return "{\"status\"=\"" + status + "\",\"message\"=\"" + message + "\"}";
}
@Override
public int hashCode() {
return toString().hashCode();
}
@Override
public boolean equals(Object obj) {
return obj instanceof SimpleMessage && toString().equals(obj.toString());
}
}
\ No newline at end of file
...@@ -40,6 +40,7 @@ ...@@ -40,6 +40,7 @@
<aspectj.version>1.7.2</aspectj.version> <aspectj.version>1.7.2</aspectj.version>
<spring.framework.version>3.2.4.RELEASE</spring.framework.version> <spring.framework.version>3.2.4.RELEASE</spring.framework.version>
<spring.security.version>3.1.4.RELEASE</spring.security.version> <spring.security.version>3.1.4.RELEASE</spring.security.version>
<spring.security.oauth2.version>1.0.0.RELEASE</spring.security.oauth2.version>
<spring.data.core.version>1.5.1.RELEASE</spring.data.core.version> <spring.data.core.version>1.5.1.RELEASE</spring.data.core.version>
<spring.data.jpa.version>1.3.2.RELEASE</spring.data.jpa.version> <spring.data.jpa.version>1.3.2.RELEASE</spring.data.jpa.version>
...@@ -353,6 +354,11 @@ ...@@ -353,6 +354,11 @@
<artifactId>owasp-java-html-sanitizer</artifactId> <artifactId>owasp-java-html-sanitizer</artifactId>
<version>r209</version> <version>r209</version>
</dependency> </dependency>
<dependency>
<groupId>org.springframework.security.oauth</groupId>
<artifactId>spring-security-oauth2</artifactId>
<version>${spring.security.oauth2.version}</version>
</dependency>
</dependencies> </dependencies>
<build> <build>
......
package org.crophub.auth.server.utils; package org.genesys2.server;
public interface ServiceEndpoints { public interface ServiceEndpoints {
......
package org.genesys2.server.exception;
public class ApplicationException extends Exception {
private static final long serialVersionUID = -3217910093684680094L;
public ApplicationException() {
super();
}
public ApplicationException(String message) {
super(message);
}
public ApplicationException(String message, Throwable cause) {
super(message, cause);
}
public ApplicationException(Throwable cause) {
super(cause);
}
}
package org.genesys2.server.exception;
public class AuthorizationException extends ApplicationException {
private static final long serialVersionUID = -5675061342788496576L;
public AuthorizationException() {
super();
}
public AuthorizationException(String message) {
super(message);
}
public AuthorizationException(String message, Throwable cause) {
super(message, cause);
}
public AuthorizationException(Throwable cause) {
super(cause);
}
}
package org.crophub.auth.server.exception; package org.genesys2.server.exception;
public class NoSuchTokenException extends RuntimeException { public class NoSuchTokenException extends RuntimeException {
public NoSuchTokenException(String message) { private static final long serialVersionUID = 2802799300427359826L;
public NoSuchTokenException(String message) {
super(message); super(message);
} }
......
package org.crophub.auth.server.model.impl; package org.genesys2.server.model.oauth;
import org.crophub.auth.server.model.HibernateModel;
import javax.persistence.*;
import java.sql.Blob; import java.sql.Blob;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.Table;
import org.genesys2.server.model.HibernateModel;
@Entity @Entity
@Table(name = "oauth_access_token") @Table(name = "oauth_access_token")
public class OAuthAccessToken implements HibernateModel { public class OAuthAccessToken implements HibernateModel {
......
package org.crophub.auth.server.model.impl; package org.genesys2.server.model.oauth;
import org.crophub.auth.server.model.BusinessModel;
import javax.persistence.Column; import javax.persistence.Column;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.Table; import javax.persistence.Table;
import org.genesys2.server.model.BusinessModel;
@Entity @Entity
@Table(name = "authorities") @Table(name = "authorities")
public class OAuthAuthorities extends BusinessModel{ public class OAuthAuthorities extends BusinessModel{
......
package org.crophub.auth.server.model.impl; 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.Id;
import javax.persistence.Table;
import javax.persistence.Transient;
import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils; import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.oauth2.provider.ClientDetails; import org.springframework.security.oauth2.provider.ClientDetails;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import javax.persistence.*;
import java.util.*;
@Entity @Entity
@Table(name = "oauth_client_details") @Table(name = "oauth_client_details")
public class OAuthClientDetails implements ClientDetails { public class OAuthClientDetails implements ClientDetails {
......
package org.crophub.auth.server.model.impl; package org.genesys2.server.model.oauth;
import org.crophub.auth.server.model.HibernateModel;
import javax.persistence.*;
import java.sql.Blob; import java.sql.Blob;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.Table;
import org.genesys2.server.model.HibernateModel;
@Entity @Entity
@Table(name = "oauth_client_token") @Table(name = "oauth_client_token")
public class OAuthClientToken implements HibernateModel { public class OAuthClientToken implements HibernateModel {
......
package org.crophub.auth.server.model.impl; package org.genesys2.server.model.oauth;
import org.crophub.auth.server.model.HibernateModel;
import javax.persistence.*;
import java.sql.Blob; import java.sql.Blob;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.Table;
import org.genesys2.server.model.HibernateModel;
@Entity @Entity
@Table(name = "oauth_code") @Table(name = "oauth_code")
public class OAuthCode implements HibernateModel { public class OAuthCode implements HibernateModel {
......
package org.crophub.auth.server.model.impl; package org.genesys2.server.model.oauth;
import org.crophub.auth.server.model.HibernateModel;
import javax.persistence.*;
import java.sql.Blob; import java.sql.Blob;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.Table;
import org.genesys2.server.model.HibernateModel;
@Entity @Entity
@Table(name = "oauth_refresh_token") @Table(name = "oauth_refresh_token")
public class OAuthRefreshToken implements HibernateModel { public class OAuthRefreshToken implements HibernateModel {
......
package org.crophub.auth.server.persistence; package org.genesys2.server.persistence.domain;
import org.crophub.auth.server.model.impl.OAuthAccessToken; import org.genesys2.server.model.oauth.OAuthAccessToken;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
......
package org.crophub.auth.server.persistence; package org.genesys2.server.persistence.domain;
import org.crophub.auth.server.model.impl.OAuthAuthorities; import org.genesys2.server.model.oauth.OAuthAuthorities;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
......
package org.crophub.auth.server.persistence; package org.genesys2.server.persistence.domain;
import org.crophub.auth.server.model.impl.OAuthClientDetails; import org.genesys2.server.model.oauth.OAuthClientDetails;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
......
package org.crophub.auth.server.persistence; package org.genesys2.server.persistence.domain;
import org.crophub.auth.server.model.impl.OAuthClientToken; import org.genesys2.server.model.oauth.OAuthClientToken;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
......
package org.crophub.auth.server.persistence; package org.genesys2.server.persistence.domain;
import org.crophub.auth.server.model.impl.OAuthCode; import org.genesys2.server.model.oauth.OAuthCode;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
......
package org.crophub.auth.server.persistence; package org.genesys2.server.persistence.domain;
import org.crophub.auth.server.model.impl.OAuthRefreshToken; import org.genesys2.server.model.oauth.OAuthRefreshToken;
import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
......
package org.crophub.auth.server.service.impl; package org.genesys2.server.service.impl;
import java.util.ArrayList;
import java.util.List;
import javax.sql.DataSource;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.crophub.auth.server.model.impl.OAuthClientDetails; import org.genesys2.server.model.oauth.OAuthClientDetails;
import org.crophub.auth.server.persistence.OAuthClientDetailsPersistence; import org.genesys2.server.persistence.domain.OAuthClientDetailsPersistence;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException; import org.springframework.dao.DuplicateKeyException;
import org.springframework.dao.EmptyResultDataAccessException; import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.security.crypto.password.NoOpPasswordEncoder; import org.springframework.security.crypto.password.NoOpPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.oauth2.common.exceptions.InvalidClientException; import org.springframework.security.oauth2.common.exceptions.InvalidClientException;
import org.springframework.security.oauth2.provider.*; import org.springframework.security.oauth2.provider.ClientAlreadyExistsException;
import org.springframework.security.oauth2.provider.ClientDetails;
import org.springframework.security.oauth2.provider.ClientDetailsService;
import org.springframework.security.oauth2.provider.ClientRegistrationService;
import org.springframework.security.oauth2.provider.NoSuchClientException;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import javax.sql.DataSource;
import java.util.ArrayList;
import java.util.List;
public class JdbcClientDetailsService implements ClientDetailsService, ClientRegistrationService { public class JdbcClientDetailsService implements ClientDetailsService, ClientRegistrationService {
private static final Log logger = LogFactory.getLog(JdbcClientDetailsService.class); private static final Log logger = LogFactory.getLog(JdbcClientDetailsService.class);
......
...@@ -26,12 +26,19 @@ import org.slf4j.LoggerFactory; ...@@ -26,12 +26,19 @@ import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource; import org.springframework.context.MessageSource;
import org.springframework.context.i18n.LocaleContextHolder; import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes; import org.springframework.web.context.request.ServletRequestAttributes;
public abstract class BaseController { public abstract class BaseController {
protected Logger _logger = LoggerFactory.getLogger(getClass()); protected Logger _logger = LoggerFactory.getLogger(getClass());
protected static final String ANONYMOUS_USER = "anonymousUser";
protected static final String EXCEPTION_NOT_AUTHORIZED = "User is not authorized.";
protected static final String EXCEPTION_NOT_ORGANIZATION_MEMBER = "User is not a member of organization.";
@Autowired @Autowired
protected MessageSource messageSource; protected MessageSource messageSource;
...@@ -43,6 +50,11 @@ public abstract class BaseController { ...@@ -43,6 +50,11 @@ public abstract class BaseController {
protected Locale getLocale() { protected Locale getLocale() {
return LocaleContextHolder.getLocale(); return LocaleContextHolder.getLocale();
} }
protected boolean isAuthenticated(){
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
return authentication != null && !ANONYMOUS_USER.equals(authentication.getName());
}
//logs exception and returns it's message //logs exception and returns it's message
protected String simpleExceptionHandler(Throwable th){ protected String simpleExceptionHandler(Throwable th){
......
package org.crophub.auth.server.controller; package org.genesys2.server.servlet.controller;
import org.crophub.auth.server.exception.AuthorizationException; import java.io.Serializable;
import org.crophub.auth.server.exception.NoSuchTokenException; import java.security.Principal;
import org.crophub.auth.server.message.SimpleMessage; import java.util.ArrayList;
import org.crophub.auth.server.utils.ServiceEndpoints; import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import org.genesys2.server.ServiceEndpoints;
import org.genesys2.server.exception.AuthorizationException;
import org.genesys2.server.exception.NoSuchTokenException;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
...@@ -20,13 +26,11 @@ import org.springframework.security.oauth2.common.exceptions.InvalidTokenExcepti ...@@ -20,13 +26,11 @@ import org.springframework.security.oauth2.common.exceptions.InvalidTokenExcepti
import org.springframework.security.oauth2.provider.OAuth2Authentication; import org.springframework.security.oauth2.provider.OAuth2Authentication;
import org.springframework.security.oauth2.provider.token.ConsumerTokenServices; import org.springframework.security.oauth2.provider.token.ConsumerTokenServices;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.PathVariable;
import java.security.Principal; import org.springframework.web.bind.annotation.RequestMapping;
import java.util.ArrayList; import org.springframework.web.bind.annotation.RequestMethod;
import java.util.Collection; import org.springframework.web.bind.annotation.ResponseBody;
import java.util.HashMap;
import java.util.Map;
@Controller @Controller
@RequestMapping(value = "/api/v0") @RequestMapping(value = "/api/v0")
...@@ -183,4 +187,42 @@ public class TokenController extends BaseController { ...@@ -183,4 +187,42 @@ public class TokenController extends BaseController {
public void setTokenServices(ConsumerTokenServices tokenServices) { public void setTokenServices(ConsumerTokenServices tokenServices) {
this.tokenServices = tokenServices; this.tokenServices = tokenServices;
} }
public static class SimpleMessage implements Serializable {
private static final long serialVersionUID = -2090423223347806024L;
private String status;
private String message;
public SimpleMessage(String status, String message) {
this.status = status;
this.message = message;
}
public String getStatus() {
return status;
}
public String getMessage() {
return message;
}
@Override
public String toString() {
return "{\"status\"=\"" + status + "\",\"message\"=\"" + message + "\"}";
}
@Override
public int hashCode() {
return toString().hashCode();
}
@Override