mirror of
https://github.com/jlengrand/dialogflow-fun.git
synced 2026-03-10 08:11:22 +00:00
Merge pull request #10 from jlengrand/backend
[WIP] Introduce backend project and rename dialogflow agent. Adds auth in backend
This commit is contained in:
@@ -3,10 +3,12 @@
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>fr.lengrand</groupId>
|
||||
<artifactId>dialogflow-fun</artifactId>
|
||||
<artifactId>dialogflow-fun-agent</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||
<maven.compiler.source>11</maven.compiler.source>
|
||||
<maven.compiler.target>11</maven.compiler.target>
|
||||
<java.version>11</java.version>
|
||||
31
dialogflow-fun-api/.gitignore
vendored
Normal file
31
dialogflow-fun-api/.gitignore
vendored
Normal file
@@ -0,0 +1,31 @@
|
||||
HELP.md
|
||||
target/
|
||||
!.mvn/wrapper/maven-wrapper.jar
|
||||
!**/src/main/**
|
||||
!**/src/test/**
|
||||
|
||||
### STS ###
|
||||
.apt_generated
|
||||
.classpath
|
||||
.factorypath
|
||||
.project
|
||||
.settings
|
||||
.springBeans
|
||||
.sts4-cache
|
||||
|
||||
### IntelliJ IDEA ###
|
||||
.idea
|
||||
*.iws
|
||||
*.iml
|
||||
*.ipr
|
||||
|
||||
### NetBeans ###
|
||||
/nbproject/private/
|
||||
/nbbuild/
|
||||
/dist/
|
||||
/nbdist/
|
||||
/.nb-gradle/
|
||||
build/
|
||||
|
||||
### VS Code ###
|
||||
.vscode/
|
||||
68
dialogflow-fun-api/pom.xml
Normal file
68
dialogflow-fun-api/pom.xml
Normal file
@@ -0,0 +1,68 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.2.0.RELEASE</version>
|
||||
<relativePath/> <!-- lookup parent from repository -->
|
||||
</parent>
|
||||
<groupId>fr.lengrand</groupId>
|
||||
<artifactId>dialogflow-fun-api</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<name>dialogflow-fun-api</name>
|
||||
<description>Bank API dummy for Dialog Flow</description>
|
||||
|
||||
<properties>
|
||||
<java.version>11</java.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>org.junit.vintage</groupId>
|
||||
<artifactId>junit-vintage-engine</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.eclipse</groupId>
|
||||
<artifactId>yasson</artifactId>
|
||||
<version>1.0.3</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.glassfish</groupId>
|
||||
<artifactId>javax.json</artifactId>
|
||||
<version>1.1.4</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>com.google.cloud.tools</groupId>
|
||||
<artifactId>appengine-maven-plugin</artifactId>
|
||||
<version>2.1.0</version>
|
||||
<configuration>
|
||||
<version>1</version>
|
||||
<projectId>GCLOUD_CONFIG</projectId>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
2
dialogflow-fun-api/src/main/appengine/app.yaml
Normal file
2
dialogflow-fun-api/src/main/appengine/app.yaml
Normal file
@@ -0,0 +1,2 @@
|
||||
runtime: java11
|
||||
instance_class: F1
|
||||
@@ -0,0 +1,34 @@
|
||||
package fr.lengrand.dialogflowfunapi;
|
||||
|
||||
import fr.lengrand.dialogflowfunapi.openbankproject.auth.Auth;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
@SpringBootApplication
|
||||
@RestController
|
||||
public class DialogflowFunApiApplication {
|
||||
|
||||
@Autowired
|
||||
private Auth auth;
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(DialogflowFunApiApplication.class, args);
|
||||
}
|
||||
|
||||
@GetMapping("/")
|
||||
public String hello() {
|
||||
return "hello world!";
|
||||
}
|
||||
|
||||
@GetMapping("/auth")
|
||||
public String auth() throws IOException, InterruptedException {
|
||||
auth.authenticate(); // TODO: Should be done on start!
|
||||
return auth.getAuthToken().isPresent()? auth.getAuthToken().get().getToken() : "No token!";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
package fr.lengrand.dialogflowfunapi.openbankproject.auth;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.net.http.HttpResponse;
|
||||
import java.util.Optional;
|
||||
|
||||
@Configuration
|
||||
@ComponentScan
|
||||
public class Auth {
|
||||
|
||||
@Autowired
|
||||
private OpenBankCredentials credentials;
|
||||
|
||||
public static final String OBP_BASE_URL = "https://psd2-api.openbankproject.com";
|
||||
public static final String LOGIN_URL = "/my/logins/direct";
|
||||
|
||||
public JSONBodyHandler<AuthToken> jsonBodyHandler = JSONBodyHandler.getHandler(AuthToken.class);
|
||||
|
||||
private Optional<AuthToken> authToken = Optional.empty();
|
||||
|
||||
public void authenticate() throws IOException, InterruptedException {
|
||||
System.out.println("Username is " + credentials.getUsername());
|
||||
System.out.println("Password is " + credentials.getPassword());
|
||||
System.out.println("Key is " + credentials.getKey());
|
||||
|
||||
|
||||
HttpClient client = HttpClient.newHttpClient();
|
||||
HttpRequest request = HttpRequest.newBuilder()
|
||||
.headers("Content-Type", "application/json",
|
||||
"Authorization", createDirectLoginHeader())
|
||||
.uri(URI.create(OBP_BASE_URL + LOGIN_URL)) // TODO : URL creation improve
|
||||
.POST(HttpRequest.BodyPublishers.ofString(""))
|
||||
.build();
|
||||
|
||||
HttpResponse<AuthToken> response = client.send(request, jsonBodyHandler); //TODO : Handle failures.
|
||||
System.out.println(response.body().getToken());
|
||||
authToken = Optional.of(response.body());
|
||||
}
|
||||
|
||||
public boolean isAuthenticated() {
|
||||
return authToken.isPresent();
|
||||
}
|
||||
|
||||
public Optional<AuthToken> getAuthToken() {
|
||||
return authToken;
|
||||
}
|
||||
|
||||
private String createDirectLoginHeader(){
|
||||
return "DirectLogin username=" + this.credentials.getUsername() + ",password="+ this.credentials.getPassword() + ",consumer_key="+ this.credentials.getKey();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
package fr.lengrand.dialogflowfunapi.openbankproject.auth;
|
||||
|
||||
public class AuthToken {
|
||||
private String token;
|
||||
|
||||
public String getToken() {
|
||||
return token;
|
||||
}
|
||||
|
||||
public void setToken(String token) {
|
||||
this.token = token;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package fr.lengrand.dialogflowfunapi.openbankproject.auth;
|
||||
|
||||
import javax.json.bind.Jsonb;
|
||||
import javax.json.bind.JsonbBuilder;
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.net.http.HttpResponse;
|
||||
import java.net.http.HttpResponse.BodySubscriber;
|
||||
import java.net.http.HttpResponse.BodySubscribers;
|
||||
import java.net.http.HttpResponse.ResponseInfo;
|
||||
import java.util.function.Function;
|
||||
|
||||
public class JSONBodyHandler<T> implements HttpResponse.BodyHandler<T> {
|
||||
private final Jsonb jsonBinder;
|
||||
private final Class<T> type;
|
||||
private HttpResponse.BodySubscriber<byte[]> byteArraySubscriber;
|
||||
|
||||
public static <T> JSONBodyHandler<T> getHandler(final Class<T> type){
|
||||
return new JSONBodyHandler<>(JsonbBuilder.create(), type);
|
||||
}
|
||||
|
||||
private JSONBodyHandler(Jsonb jsonBinder, Class<T> type){
|
||||
this.jsonBinder = jsonBinder;
|
||||
this.type = type;
|
||||
this.byteArraySubscriber = BodySubscribers.ofByteArray();
|
||||
}
|
||||
|
||||
@Override
|
||||
public BodySubscriber<T> apply(ResponseInfo responseInfo) {
|
||||
return HttpResponse.BodySubscribers.mapping(byteArraySubscriber, byteArrayToJSON());
|
||||
}
|
||||
|
||||
private Function<byte[], T> byteArrayToJSON() {
|
||||
return byteArray -> this.jsonBinder.fromJson(new ByteArrayInputStream(byteArray), this.type);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
package fr.lengrand.dialogflowfunapi.openbankproject.auth;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
@ComponentScan
|
||||
@Configuration
|
||||
public class OpenBankCredentials {
|
||||
|
||||
@Value("${obp.username}")
|
||||
private String username;
|
||||
|
||||
@Value("${obp.password}")
|
||||
private String password;
|
||||
|
||||
@Value("${obp.key}")
|
||||
private String key;
|
||||
|
||||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
|
||||
public String getKey() {
|
||||
return key;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
obp.username=${OPENBANKPROJECT_USERNAME}
|
||||
obp.password=${OPENBANKPROJECT_PASSWORD}
|
||||
obp.key=${OPENBANKPROJECT_CONSUMERKEY}
|
||||
@@ -0,0 +1,13 @@
|
||||
package fr.lengrand.dialogflowfunapi;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
|
||||
@SpringBootTest
|
||||
class DialogflowFunApiApplicationTests {
|
||||
|
||||
@Test
|
||||
void contextLoads() {
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,23 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<projectDescription>
|
||||
<name>dialogflow-fun</name>
|
||||
<comment></comment>
|
||||
<projects>
|
||||
</projects>
|
||||
<buildSpec>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.jdt.core.javabuilder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
<buildCommand>
|
||||
<name>org.eclipse.m2e.core.maven2Builder</name>
|
||||
<arguments>
|
||||
</arguments>
|
||||
</buildCommand>
|
||||
</buildSpec>
|
||||
<natures>
|
||||
<nature>org.eclipse.jdt.core.javanature</nature>
|
||||
<nature>org.eclipse.m2e.core.maven2Nature</nature>
|
||||
</natures>
|
||||
</projectDescription>
|
||||
@@ -1,2 +0,0 @@
|
||||
eclipse.preferences.version=1
|
||||
org.eclipse.jdt.apt.aptEnabled=false
|
||||
@@ -1,9 +0,0 @@
|
||||
eclipse.preferences.version=1
|
||||
org.eclipse.jdt.core.compiler.codegen.targetPlatform=11
|
||||
org.eclipse.jdt.core.compiler.compliance=11
|
||||
org.eclipse.jdt.core.compiler.problem.enablePreviewFeatures=disabled
|
||||
org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning
|
||||
org.eclipse.jdt.core.compiler.problem.reportPreviewFeatures=ignore
|
||||
org.eclipse.jdt.core.compiler.processAnnotations=disabled
|
||||
org.eclipse.jdt.core.compiler.release=disabled
|
||||
org.eclipse.jdt.core.compiler.source=11
|
||||
@@ -1,4 +0,0 @@
|
||||
activeProfiles=
|
||||
eclipse.preferences.version=1
|
||||
resolveWorkspaceProjects=true
|
||||
version=1
|
||||
@@ -1,2 +0,0 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="JAVA_MODULE" version="4" />
|
||||
5
pom.xml
5
pom.xml
@@ -8,12 +8,15 @@
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||
<maven.compiler.source>11</maven.compiler.source>
|
||||
<maven.compiler.target>11</maven.compiler.target>
|
||||
<java.version>11</java.version>
|
||||
</properties>
|
||||
|
||||
<modules>
|
||||
<module>dialogflow-fun-java</module>
|
||||
<module>dialogflow-fun-agent</module>
|
||||
<module>dialogflow-fun-api</module>
|
||||
</modules>
|
||||
</project>
|
||||
Reference in New Issue
Block a user