Commit 106374ed authored by Sebastian's avatar Sebastian

OAuth-Login, osmapi, automatisches anlegen von unprivilegierten benutzern.

parent 477e59cb
package de.transformationsstadt.geoportal.DAO;
import java.util.List;
import de.transformationsstadt.geoportal.entities.OAuthToken;
public interface OAuthTokenDAO extends DaoInterface<OAuthToken>{
public OAuthToken get(String token);
}
package de.transformationsstadt.geoportal.DAO;
import java.util.List;
import org.springframework.stereotype.Repository;
import de.transformationsstadt.geoportal.entities.OAuthToken;
@Repository
public class OAuthTokenDAOImpl extends GenericDao<OAuthToken> implements OAuthTokenDAO {
public OAuthToken get(String token)
{
return getSingleResultByField("OAuthToken",token);
}
}
package de.transformationsstadt.geoportal;
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.security.Key;
import java.util.Properties;
import javax.crypto.spec.SecretKeySpec;
import javax.xml.bind.DatatypeConverter;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import de.transformationsstadt.geoportal.auth.JwtUtil;
import de.transformationsstadt.geoportal.config.OAuthParameters;
import io.jsonwebtoken.SignatureAlgorithm;
public class Settings {
public final static String propertiesFile = "geoportal.properties";
public final static String propertiesFile = "geoportal.properties.xml";
private final static String oAuthParametersFile = "OAuthParameters.xml";
private Boolean userRegistrationEnabled = null;
private Boolean debugModeEnabled = null;
private String osmApiUrl = "";
private static Settings instance = null;
private static OAuthParameters oAuthParameters;
public String inputStreamToString(InputStream is) throws IOException {
StringBuilder sb = new StringBuilder();
String line;
BufferedReader br = new BufferedReader(new InputStreamReader(is));
while ((line = br.readLine()) != null) {
sb.append(line);
}
br.close();
return sb.toString();
}
public String getOsmApiURL() {
return this.osmApiUrl;
}
public OAuthParameters OAuthParameters(){
return Settings.oAuthParameters;
}
private Settings() {
InputStream is = getClass().getClassLoader().getResourceAsStream(propertiesFile);
Properties properties = new Properties();
try{
properties.load(is);
System.out.println("Loading properties from "+propertiesFile+": ");
properties.loadFromXML(is);
System.out.println("Properties loaded.");
String ureString = (String)properties.getProperty("userRegistration.enabled");
osmApiUrl = (String)properties.getProperty("osm.apiURL");
this.userRegistrationEnabled = (ureString.toLowerCase().equals("true"));
String dbgString = (String)properties.getProperty("debug");
this.debugModeEnabled = (ureString.toLowerCase().equals("true"));
if(this.userRegistrationEnabled) {
System.out.println("User registration enabled.");
}else {
System.out.println("User registration disabled. config-item: ["+ureString+"]");
}
}catch(IOException ioe) {
}catch(IOException ioe) {
System.out.println("Error Loading "+propertiesFile+": "+ioe.getMessage());
}
try {
XmlMapper xmlMapper = new XmlMapper();
String xml = inputStreamToString(getClass().getClassLoader().getResourceAsStream(oAuthParametersFile));
OAuthParameters p = xmlMapper.readValue(xml,OAuthParameters.class);
Settings.oAuthParameters = p;
System.out.println("Oauth loaded: "+oAuthParameters.toString());
}catch(IOException ioe) {
System.out.println("Error Loading "+oAuthParametersFile+": "+ioe.getMessage());
}
......@@ -42,6 +86,9 @@ public class Settings {
}
return instance;
}
public Boolean debugMode() {
return this.debugModeEnabled;
}
public Boolean userRegistrationEnabled() {
return this.userRegistrationEnabled;
}
......
......@@ -209,12 +209,12 @@ public class Accounts {
}
}
/**
* Legt einen neuen Benutzer an.
*
* Der neue Benutzer wird der Rolle "user" zugeordnet.
* Der neue Benutzer wird der Rolle "user" zugeordnet.
*
* TODO: EMail-Verifikation
* @param user {@link UserParameters}
......@@ -233,7 +233,7 @@ public class Accounts {
/**
* Registrierung abgeschaltet.
*/
System.out.println("Register...");
if(!Settings.getInstance().userRegistrationEnabled()){
map.put("error","user registration has been disabled");
return Response.status(Status.SERVICE_UNAVAILABLE).entity(map).build();
......
This diff is collapsed.
......@@ -91,6 +91,7 @@ public class BearerAuthenticatingFilter extends AuthenticatingFilter{
if(currentMethod.equals("OPTIONS")) {
return true;
}
if(currentMethod.equals("GET") && !currentPath.toLowerCase().startsWith("/accounts/")) {
return true;
}
......
package de.transformationsstadt.geoportal.auth.oauth;
import com.github.scribejava.core.builder.ServiceBuilder;
import com.github.scribejava.core.builder.api.DefaultApi10a;
import com.github.scribejava.core.oauth.OAuth10aService;
import de.transformationsstadt.geoportal.Settings;
import de.transformationsstadt.geoportal.config.OAuthParameters;
public class ConfiguredOAuthApi10a extends DefaultApi10a {
OAuthParameters parameters;
public ConfiguredOAuthApi10a(OAuthParameters oap) {
if(!oap.getVersion().equals("1.0a")) {
throw new RuntimeException(this.getClass() + " needs to be instantiated with an oAuth-configuration for version 1.0a, got "+oap.getVersion());
}
this.parameters = oap;
}
public OAuth10aService getService() {
System.out.println("Creating service.");
return (OAuth10aService) (
new ServiceBuilder(parameters.getConsumerKey())
.apiSecret(parameters.getConsumerSecret())
.build(this)
);
}
public ConfiguredOAuthApi10a() {
System.out.println("Loading oAuth-Parameters from config.");
OAuthParameters oap = Settings.getInstance().OAuthParameters();
if( oap == null ) {
System.out.println("oap null.");
}
if(!oap.getVersion().equals("1.0a")) {
throw new RuntimeException(this.getClass() + " needs to be instantiated with an oAuth-configuration for version 1.0a, got "+oap.getVersion());
}
this.parameters = oap;
}
public OAuthParameters getOAuthParameters() {
return this.parameters;
}
@Override
public String getRequestTokenEndpoint() {
return parameters.getRequestTokenURL();
}
@Override
public String getAccessTokenEndpoint() {
return parameters.getAccessTokenURL();
}
@Override
protected String getAuthorizationBaseUrl() {
return parameters.getAuthorizeURL();
}
}
package de.transformationsstadt.geoportal.config;
public class OAuthParameters {
private String requestTokenURL = "";
private String accessTokenURL = "";
private String authorizeURL = "";
private String consumerKey = "";
private String consumerSecret = "";
private String name = "";
private String version = "";
private String callback = "";
public String getCallback() {
return callback;
}
public void setCallback(String cb) {
callback = cb;
}
public String getVersion() {
return version;
}
public void setVersion(String oAuthVersion) {
this.version = oAuthVersion;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getRequestTokenURL() {
return requestTokenURL;
}
public void setRequestTokenURL(String requestTokenURL) {
this.requestTokenURL = requestTokenURL;
}
public String getAccessTokenURL() {
return accessTokenURL;
}
public void setAccessTokenURL(String accessTokenURL) {
this.accessTokenURL = accessTokenURL;
}
public String getAuthorizeURL() {
return authorizeURL;
}
public void setAuthorizeURL(String authorizeURL) {
this.authorizeURL = authorizeURL;
}
public String getConsumerKey() {
return consumerKey;
}
public void setConsumerKey(String consumerKey) {
this.consumerKey = consumerKey;
}
public String getConsumerSecret() {
return consumerSecret;
}
public void setConsumerSecret(String consumerSecret) {
this.consumerSecret = consumerSecret;
}
public String toString(){
String str = "";
str += "Name: ["+name+"], ConsumerKey: ["+consumerKey+"], requestTokenURL: ["+requestTokenURL+"], accessTokenURL: ["+accessTokenURL+"], authorizeURL: ["+authorizeURL+"]";
return str;
}
}
package de.transformationsstadt.geoportal.entities;
import java.io.Serializable;
import java.util.Date;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.OneToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.NotNull;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.Status;
import org.hibernate.annotations.CreationTimestamp;
import com.github.scribejava.core.model.OAuth1AccessToken;
import de.transformationsstadt.geoportal.auth.oauth.ConfiguredOAuthApi10a;
import de.transformationsstadt.geoportal.config.OAuthParameters;
import oauth.signpost.OAuthConsumer;
import oauth.signpost.basic.DefaultOAuthConsumer;
import oauth.signpost.signature.OAuthMessageSigner;
//PSQL: CREATE TABLE oauth_tokens ( id bigint NOT NULL, created timestamp without time zone NOT NULL, oauthtoken VARCHAR(255), oauthtokenverifier VARCHAR(255), oauthtokensecret VARCHAR(255), accesstoken VARCHAR(255), accesstokensecret VARCHAR(255), user_id bigint);
@Entity
@Table(name="oauth_tokens")
public class OAuthToken implements Serializable{
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue
private Long id;
@NotNull
@CreationTimestamp
@Column(name = "created", updatable=false)
private Date created;
@OneToOne
@JoinColumn(name = "user_id")
private User user;
private String oauthToken;
private String oauthTokenSecret;
private String oauthTokenVerifier;
private String accessToken;
private String accessTokenSecret;
public OAuthToken() {
created = new Date();
}
public User getUser() {
return user;
}
public void setUser(User user) {
this.user = user;
}
public OAuthToken(String oauthToken, String oauthTokenSecret,String oauthTokenVerifier) {
setOauthToken(oauthToken);
setOauthTokenSecret(oauthTokenSecret);
setOauthTokenVerifier(oauthTokenVerifier);
created = new Date();
}
public Boolean hasAccessToken() {
return this.accessToken != null && !this.accessToken.isEmpty();
}
public OAuth1AccessToken getOAuth1AccessToken() {
return new OAuth1AccessToken(this.accessToken,this.accessTokenSecret);
}
public OAuthConsumer getSignPostConsumer() {
OAuthParameters oap = (new ConfiguredOAuthApi10a()).getOAuthParameters();
System.out.println("Setting consumer key to "+oap.getConsumerKey());
OAuthConsumer consumer = new DefaultOAuthConsumer(oap.getConsumerKey(), oap.getConsumerSecret());
consumer.setTokenWithSecret(this.accessToken, this.accessTokenSecret);
return consumer;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Date getCreated() {
return created;
}
public void setCreated(Date created) {
this.created = created;
}
public String getOauthToken() {
return oauthToken;
}
public void setOauthToken(String oauthToken) {
this.oauthToken = oauthToken;
}
public String getOauthTokenSecret() {
return oauthTokenSecret;
}
public void setOauthTokenSecret(String oauthTokenSecret) {
this.oauthTokenSecret = oauthTokenSecret;
}
public String getOauthTokenVerifier() {
return oauthTokenVerifier;
}
public void setOauthTokenVerifier(String oauthTokenVerifier) {
this.oauthTokenVerifier = oauthTokenVerifier;
}
public String getAccessToken() {
return accessToken;
}
public void setAccessToken(String accessToken) {
this.accessToken = accessToken;
}
public String getAccessTokenSecret() {
return accessTokenSecret;
}
public void setAccessTokenSecret(String accessTokenSecret) {
this.accessTokenSecret = accessTokenSecret;
}
}
package de.transformationsstadt.geoportal.services;
import java.util.List;
import de.transformationsstadt.geoportal.entities.OAuthToken;
public interface OAuthTokenService extends ServiceInterface<OAuthToken>{
public OAuthToken get(Long id);
public OAuthToken get(String token);
public OAuthToken create(OAuthToken t);
public List<OAuthToken> getAll();
public OAuthToken merge(OAuthToken t);
public OAuthToken update(OAuthToken t);
}
package de.transformationsstadt.geoportal.services;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import de.transformationsstadt.geoportal.DAO.OAuthTokenDAO;
import de.transformationsstadt.geoportal.entities.App;
import de.transformationsstadt.geoportal.entities.OAuthToken;
@Service
@Transactional
public class OAuthTokenServiceImpl extends GenericService<OAuthToken> implements OAuthTokenService{
@Autowired
OAuthTokenDAO dao;
public OAuthToken get(Long id) {
return dao.get(id);
}
public OAuthToken get(String t) {
return dao.getSingleResultByField("oauthToken", t);
}
public OAuthToken create(OAuthToken t) {
long id = (long) dao.create(t);
t.setId(id);
return t;
}
public List<OAuthToken> getAll() {
return dao.getAll();
}
public OAuthToken merge(OAuthToken t) {
dao.merge(t);
return t;
}
public OAuthToken update(OAuthToken t) {
t = dao.update(t);
return t;
}
}
<OAuthParameters>
<version>1.0a</version>
<requestTokenURL>https://master.apis.dev.openstreetmap.org/oauth/request_token</requestTokenURL>
<accessTokenURL>https://master.apis.dev.openstreetmap.org/oauth/access_token</accessTokenURL>
<authorizeURL>https://master.apis.dev.openstreetmap.org/oauth/authorize</authorizeURL>
<consumerKey>TjlQ0FPg4nKNMvtKngT8PeJyc0uk7i2M3LIM6wKJ</consumerKey>
<consumerSecret>y9YuJBM7udADgIGLLPXTyU2yac2S0z30QCDY7RKM</consumerSecret>
<name>Geoportal</name>
</OAuthParameters>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<entry key="jwt.key">i am a secret key and should be changed for deployment</entry>
<entry key="jwt.issuerPrefix">geoportal</entry>
<entry key="userRegistration.enabled">false</entry>
<entry key="mail.smtp.host">your_smtp_host</entry>
<entry key="mail.from">your_mail_sender</entry>
<entry key="mail.receiver">your_mail_receiver</entry>
<entry key="mail.smtp.auth">true</entry>
<entry key="mail.smtp.port">465</entry>
<entry key="mail.user">your_mail_user</entry>
<entry key="mail.password">your_password</entry>
<entry key="jwt.key">i am a secret key and should be changed for deployment</entry>
<entry key="jwt.issuerPrefix">geoportal</entry>
<entry key="osm.apiURL">https://master.apis.dev.openstreetmap.org/api/0.6/</entry>
<entry key="osm.apiURL_live">https://api.openstreetmap.org/api/0.6/</entry>
<entry key="userRegistration.enabled">true</entry>
<entry key="debug">true</entry>
<entry key="mail.smtp.host">your_smtp_host</entry>
<entry key="mail.from">your_mail_sender</entry>
<entry key="mail.receiver">your_mail_receiver</entry>
<entry key="mail.smtp.auth">true</entry>
<entry key="mail.smtp.port">465</entry>
<entry key="mail.user">your_mail_user</entry>
<entry key="mail.password">your_password</entry>
</properties>
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment