From 8c792c687adf32252533581ca40bba7a5aafa7a0 Mon Sep 17 00:00:00 2001 From: Ethan Hunter Date: Thu, 5 Oct 2017 11:53:40 -0600 Subject: [PATCH 01/47] begin work on security features --- build.gradle | 3 ++- .../org/x2b/study/core/security/User.java | 10 ++++++++ .../study/core/security/data/Permission.java | 14 +++++++++++ .../security/data/PermissionRepository.java | 18 +++++++++++++ .../data/UserPermissionsDocument.java | 25 +++++++++++++++++++ 5 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 src/main/java/org/x2b/study/core/security/User.java create mode 100644 src/main/java/org/x2b/study/core/security/data/Permission.java create mode 100644 src/main/java/org/x2b/study/core/security/data/PermissionRepository.java create mode 100644 src/main/java/org/x2b/study/core/security/data/UserPermissionsDocument.java diff --git a/build.gradle b/build.gradle index 79e2a72..ab3068d 100644 --- a/build.gradle +++ b/build.gradle @@ -5,6 +5,7 @@ buildscript { repositories { maven { url "https://plugins.gradle.org/m2/" } maven { url 'http://repo.spring.io/plugins-release' } + maven {url 'https://repo.spring.io/libs-release'} } dependencies { classpath("org.springframework.boot:spring-boot-gradle-plugin:1.5.7.RELEASE") @@ -38,5 +39,5 @@ dependencies { compile 'com.graphql-java:graphiql-spring-boot-starter:3.9.2' - compile 'com.graphql-java:graphql-java-tools:4.1.2' + compile 'org.springframework.data:spring-data-mongodb:2.0.0.RELEASE' } diff --git a/src/main/java/org/x2b/study/core/security/User.java b/src/main/java/org/x2b/study/core/security/User.java new file mode 100644 index 0000000..9225498 --- /dev/null +++ b/src/main/java/org/x2b/study/core/security/User.java @@ -0,0 +1,10 @@ +package org.x2b.study.core.security; + +import java.util.UUID; + +public interface User { + + UUID getUUID(); + String getUsername(); + String getSecret(); +} diff --git a/src/main/java/org/x2b/study/core/security/data/Permission.java b/src/main/java/org/x2b/study/core/security/data/Permission.java new file mode 100644 index 0000000..9c3009d --- /dev/null +++ b/src/main/java/org/x2b/study/core/security/data/Permission.java @@ -0,0 +1,14 @@ +package org.x2b.study.core.security.data; + +public class Permission { + + private final String permission; + + public Permission(String permission) { + this.permission = permission; + } + + public String getPermission() { + return permission; + } +} diff --git a/src/main/java/org/x2b/study/core/security/data/PermissionRepository.java b/src/main/java/org/x2b/study/core/security/data/PermissionRepository.java new file mode 100644 index 0000000..cc3f248 --- /dev/null +++ b/src/main/java/org/x2b/study/core/security/data/PermissionRepository.java @@ -0,0 +1,18 @@ +package org.x2b.study.core.security.data; + +import org.x2b.study.core.security.User; + +import java.util.Collection; + +public interface PermissionRepository { + + boolean isUserPresent(User user); + + boolean addUser(User user); + + boolean addPermissionToUser(User user, Permission permission); + + boolean addPermissionsToUser(User user, Collection permissions); + + boolean removePermissionFromUser(User user, Permission permission); +} diff --git a/src/main/java/org/x2b/study/core/security/data/UserPermissionsDocument.java b/src/main/java/org/x2b/study/core/security/data/UserPermissionsDocument.java new file mode 100644 index 0000000..e1e0f17 --- /dev/null +++ b/src/main/java/org/x2b/study/core/security/data/UserPermissionsDocument.java @@ -0,0 +1,25 @@ +package org.x2b.study.core.security.data; + +import java.util.HashSet; +import java.util.Set; +import java.util.UUID; + +public class UserPermissionsDocument { + + private final UUID userId; + private final Set permissions; + + + public UserPermissionsDocument(UUID userId, Set permissions) { + this.userId = userId; + this.permissions = new HashSet<>(permissions); + } + + public boolean hasPermission(String permission) { + return permission.contains(permission); + } + + public UUID getUserId() { + return userId; + } +} From f915cc4107ad113c3ccdad28509eb5ef655b2d30 Mon Sep 17 00:00:00 2001 From: Ethan Hunter Date: Thu, 5 Oct 2017 12:15:24 -0600 Subject: [PATCH 02/47] design inital auth interface --- .../study/core/security/data/Permission.java | 13 +++ .../security/data/PermissionRepository.java | 90 ++++++++++++++++++- .../UserPermissionsDocument.java | 2 +- 3 files changed, 100 insertions(+), 5 deletions(-) rename src/main/java/org/x2b/study/core/security/data/{ => mongodb}/UserPermissionsDocument.java (91%) diff --git a/src/main/java/org/x2b/study/core/security/data/Permission.java b/src/main/java/org/x2b/study/core/security/data/Permission.java index 9c3009d..c8ff82c 100644 --- a/src/main/java/org/x2b/study/core/security/data/Permission.java +++ b/src/main/java/org/x2b/study/core/security/data/Permission.java @@ -11,4 +11,17 @@ public Permission(String permission) { public String getPermission() { return permission; } + + @Override + public boolean equals(Object other) { //this could thrown a null ptr if permission is null + if (other != null && other instanceof Permission) { + return this.permission.equals(((Permission) other).permission); + } + return false; + } + + @Override + public int hashCode() { + return permission.hashCode(); + } } diff --git a/src/main/java/org/x2b/study/core/security/data/PermissionRepository.java b/src/main/java/org/x2b/study/core/security/data/PermissionRepository.java index cc3f248..88a1108 100644 --- a/src/main/java/org/x2b/study/core/security/data/PermissionRepository.java +++ b/src/main/java/org/x2b/study/core/security/data/PermissionRepository.java @@ -2,17 +2,99 @@ import org.x2b.study.core.security.User; -import java.util.Collection; +import java.util.Map; +import java.util.Set; public interface PermissionRepository { - boolean isUserPresent(User user); + /** + * Creates a user in the repository with no permissions + * @param user the user that will be added + * @return true iff successful + * + * @implNote The only stable field of user is the UUID + */ + boolean createUser(User user); - boolean addUser(User user); + /** + * Remove a user from the repository + * @param user the user that will be removed + * @return success + */ + boolean removeUser(User user); + + /** + * Update a user in the repository with a new permission + * @param user The user to be updated + * @param permission The permission to add + * @return success + */ boolean addPermissionToUser(User user, Permission permission); - boolean addPermissionsToUser(User user, Collection permissions); + /** + * Update a user in the repository with a new permission + * @param user The user to be updated + * @param permissions The permissions to add + * @return success + */ + boolean addPermissionsToUser(User user, Set permissions); + /** + * Update a user in the repository without a permission + * @param user The user to be updated + * @param permission The permission to remove + * @return success + */ boolean removePermissionFromUser(User user, Permission permission); + + /** + * Update a user in the repository without a permission + * @param user The user to be updated + * @param permissions The permissions to remove + * @return success + */ + boolean removePermissionsFromUser(User user, Set permissions); + + /** + * Check if a user is present in the repository + * @param user the user to check + * @return true if the user exists, false otherwise + */ + boolean doesUserExist(User user); + + /** + * Check if a user has a permission + * @param user The user to read + * @param permission The permission to check + * @return true iff the user has the permission + */ + boolean doesUserHavePermission(User user, Permission permission); + + /** + * Check if a user has a set of permissions + * @param user The user to check + * @param permissions The set of permissions + * @return A map where each permission in the initial set is a key. Each value is true + * if the user has the permission and false otherwise. + * Returns null if the user does not exist in the repository + */ + Map doesUserHavePermissions(User user, Set permissions); + + /** + * Check if a user has every permission in a set + * @param user The user to read + * @param permissions the set of permissions + * @return true iff the user has every permission in the set + */ + boolean doesUserHaveAllPermissions(User user, Set permissions); + + + /** + * Get a set containing all of the users permissions + * @param user The user to read + * @return The set of permissions or null if the User does not exist in the + * Repository + */ + Set getAllPermissionsOfUser(User user); } diff --git a/src/main/java/org/x2b/study/core/security/data/UserPermissionsDocument.java b/src/main/java/org/x2b/study/core/security/data/mongodb/UserPermissionsDocument.java similarity index 91% rename from src/main/java/org/x2b/study/core/security/data/UserPermissionsDocument.java rename to src/main/java/org/x2b/study/core/security/data/mongodb/UserPermissionsDocument.java index e1e0f17..8fcc2f1 100644 --- a/src/main/java/org/x2b/study/core/security/data/UserPermissionsDocument.java +++ b/src/main/java/org/x2b/study/core/security/data/mongodb/UserPermissionsDocument.java @@ -1,4 +1,4 @@ -package org.x2b.study.core.security.data; +package org.x2b.study.core.security.data.mongodb; import java.util.HashSet; import java.util.Set; From af2e37546d20c7b7816bfc77f14b65f4989a1a18 Mon Sep 17 00:00:00 2001 From: Ethan Hunter Date: Thu, 5 Oct 2017 14:26:04 -0600 Subject: [PATCH 03/47] add unit test for schema creation --- .../org/x2b/study/core/security/User.java | 3 -- .../security/data/PermissionRepository.java | 2 + .../data/mongodb/AuthenticatedUser.java | 35 +++++++++++++ .../data/mongodb/AuthorizationRepository.java | 8 +++ .../data/mongodb/UserPermissionsDocument.java | 25 --------- .../core/TestGraphQLServiceConfigure.java | 52 +++++++++++++++++++ .../org/x2b/study/core/graphql/Hello.java | 12 +++++ src/test/resources/schema.gql | 15 ++---- 8 files changed, 114 insertions(+), 38 deletions(-) create mode 100644 src/main/java/org/x2b/study/core/security/data/mongodb/AuthenticatedUser.java create mode 100644 src/main/java/org/x2b/study/core/security/data/mongodb/AuthorizationRepository.java delete mode 100644 src/main/java/org/x2b/study/core/security/data/mongodb/UserPermissionsDocument.java create mode 100644 src/test/java/org/x2b/study/core/TestGraphQLServiceConfigure.java create mode 100644 src/test/java/org/x2b/study/core/graphql/Hello.java diff --git a/src/main/java/org/x2b/study/core/security/User.java b/src/main/java/org/x2b/study/core/security/User.java index 9225498..e3ea060 100644 --- a/src/main/java/org/x2b/study/core/security/User.java +++ b/src/main/java/org/x2b/study/core/security/User.java @@ -3,8 +3,5 @@ import java.util.UUID; public interface User { - UUID getUUID(); - String getUsername(); - String getSecret(); } diff --git a/src/main/java/org/x2b/study/core/security/data/PermissionRepository.java b/src/main/java/org/x2b/study/core/security/data/PermissionRepository.java index 88a1108..054a410 100644 --- a/src/main/java/org/x2b/study/core/security/data/PermissionRepository.java +++ b/src/main/java/org/x2b/study/core/security/data/PermissionRepository.java @@ -1,10 +1,12 @@ package org.x2b.study.core.security.data; +import org.springframework.stereotype.Repository; import org.x2b.study.core.security.User; import java.util.Map; import java.util.Set; +@Repository public interface PermissionRepository { /** diff --git a/src/main/java/org/x2b/study/core/security/data/mongodb/AuthenticatedUser.java b/src/main/java/org/x2b/study/core/security/data/mongodb/AuthenticatedUser.java new file mode 100644 index 0000000..308fe13 --- /dev/null +++ b/src/main/java/org/x2b/study/core/security/data/mongodb/AuthenticatedUser.java @@ -0,0 +1,35 @@ +package org.x2b.study.core.security.data.mongodb; + +import org.springframework.data.annotation.Id; +import org.x2b.study.core.security.User; + +import java.util.HashSet; +import java.util.Set; +import java.util.UUID; + +public class AuthenticatedUser implements User{ + + @Id + private final UUID uuid; + + private final Set permissions; + + public AuthenticatedUser(UUID uuid, Set permissions) { + this.uuid = uuid; + this.permissions = new HashSet<>(permissions); + } + + public boolean hasPermission(String permission) { + return permission.contains(permission); + } + + @Override + public UUID getUUID() { + return uuid; + } + + @Override + public String toString() { + return String.format("User: %s", uuid); + } +} diff --git a/src/main/java/org/x2b/study/core/security/data/mongodb/AuthorizationRepository.java b/src/main/java/org/x2b/study/core/security/data/mongodb/AuthorizationRepository.java new file mode 100644 index 0000000..71e4f5c --- /dev/null +++ b/src/main/java/org/x2b/study/core/security/data/mongodb/AuthorizationRepository.java @@ -0,0 +1,8 @@ +package org.x2b.study.core.security.data.mongodb; + +import org.springframework.data.mongodb.repository.MongoRepository; + +import java.util.UUID; + +public interface AuthorizationRepository extends MongoRepository { +} diff --git a/src/main/java/org/x2b/study/core/security/data/mongodb/UserPermissionsDocument.java b/src/main/java/org/x2b/study/core/security/data/mongodb/UserPermissionsDocument.java deleted file mode 100644 index 8fcc2f1..0000000 --- a/src/main/java/org/x2b/study/core/security/data/mongodb/UserPermissionsDocument.java +++ /dev/null @@ -1,25 +0,0 @@ -package org.x2b.study.core.security.data.mongodb; - -import java.util.HashSet; -import java.util.Set; -import java.util.UUID; - -public class UserPermissionsDocument { - - private final UUID userId; - private final Set permissions; - - - public UserPermissionsDocument(UUID userId, Set permissions) { - this.userId = userId; - this.permissions = new HashSet<>(permissions); - } - - public boolean hasPermission(String permission) { - return permission.contains(permission); - } - - public UUID getUserId() { - return userId; - } -} diff --git a/src/test/java/org/x2b/study/core/TestGraphQLServiceConfigure.java b/src/test/java/org/x2b/study/core/TestGraphQLServiceConfigure.java new file mode 100644 index 0000000..7008ada --- /dev/null +++ b/src/test/java/org/x2b/study/core/TestGraphQLServiceConfigure.java @@ -0,0 +1,52 @@ +package org.x2b.study.core; + +import graphql.GraphQL; +import graphql.schema.DataFetcher; +import graphql.schema.DataFetchingEnvironment; +import graphql.schema.GraphQLSchema; +import graphql.schema.GraphQLType; +import graphql.schema.idl.RuntimeWiring; +import org.junit.Assert; +import org.junit.Test; +import org.x2b.study.core.graphql.Hello; + +import java.util.List; + +public class TestGraphQLServiceConfigure { + + private class TestConfigure extends GraphQLServiceConfigure { + + @Override + protected RuntimeWiring createRuntimeWiring() { + return RuntimeWiring.newRuntimeWiring() + .type("QueryRoot", w -> w + .dataFetcher("getHello", environment -> { + return new Hello(); + }) + ) + .type("MutationRoot", w -> w + .dataFetcher("logAString", env -> "mutation") + ) + .build(); + } + } + + + @Test + public void testCreateSchema() { + TestConfigure configure = new TestConfigure(); + GraphQLSchema schema = configure.schema(); + Assert.assertNotNull(schema); + + GraphQLType queryRoot = schema.getQueryType(); + Assert.assertNotNull(queryRoot); + Assert.assertEquals("QueryRoot", queryRoot.getName()); + + GraphQLType mutRoot = schema.getMutationType(); + Assert.assertNotNull(mutRoot); + Assert.assertEquals("MutationRoot", mutRoot.getName()); + + List types = schema.getAllTypesAsList(); + Assert.assertEquals(types.toString(), 3 + 10, types.size()); //10 built in + } +} diff --git a/src/test/java/org/x2b/study/core/graphql/Hello.java b/src/test/java/org/x2b/study/core/graphql/Hello.java new file mode 100644 index 0000000..16866eb --- /dev/null +++ b/src/test/java/org/x2b/study/core/graphql/Hello.java @@ -0,0 +1,12 @@ +package org.x2b.study.core.graphql; + +public class Hello { + + public Hello() { + + } + + public String getValue() { + return "this class is for testing schema creation but does not contain tests"; + } +} diff --git a/src/test/resources/schema.gql b/src/test/resources/schema.gql index 6a2df83..da8688a 100644 --- a/src/test/resources/schema.gql +++ b/src/test/resources/schema.gql @@ -1,22 +1,17 @@ type Hello { value: String! - getComplexThing(bar: String!): String! } -input HelloInput { - foo: String! +type QueryRoot { + getHello: Hello! } -type Query { - hello(input: HelloInput!): Hello! -} - -type Mutation { +type MutationRoot { logAString(str: String!): String! } schema { - query: Query - mutation: Mutation + query: QueryRoot + mutation: MutationRoot } \ No newline at end of file From 00bd3a994678776fd8083ec1064b869425ddf61a Mon Sep 17 00:00:00 2001 From: Ethan Hunter Date: Thu, 5 Oct 2017 14:27:38 -0600 Subject: [PATCH 04/47] move supporting unit test classes into one file --- .../study/core/TestGraphQLServiceConfigure.java | 16 ++++++++++++---- .../java/org/x2b/study/core/graphql/Hello.java | 12 ------------ 2 files changed, 12 insertions(+), 16 deletions(-) delete mode 100644 src/test/java/org/x2b/study/core/graphql/Hello.java diff --git a/src/test/java/org/x2b/study/core/TestGraphQLServiceConfigure.java b/src/test/java/org/x2b/study/core/TestGraphQLServiceConfigure.java index 7008ada..4439874 100644 --- a/src/test/java/org/x2b/study/core/TestGraphQLServiceConfigure.java +++ b/src/test/java/org/x2b/study/core/TestGraphQLServiceConfigure.java @@ -1,20 +1,28 @@ package org.x2b.study.core; -import graphql.GraphQL; -import graphql.schema.DataFetcher; -import graphql.schema.DataFetchingEnvironment; import graphql.schema.GraphQLSchema; import graphql.schema.GraphQLType; import graphql.schema.idl.RuntimeWiring; import org.junit.Assert; import org.junit.Test; -import org.x2b.study.core.graphql.Hello; import java.util.List; public class TestGraphQLServiceConfigure { + + private class TestConfigure extends GraphQLServiceConfigure { + public class Hello { + + public Hello() { + + } + + public String getValue() { + return "this class is for testing schema creation but does not contain tests"; + } + } @Override protected RuntimeWiring createRuntimeWiring() { diff --git a/src/test/java/org/x2b/study/core/graphql/Hello.java b/src/test/java/org/x2b/study/core/graphql/Hello.java deleted file mode 100644 index 16866eb..0000000 --- a/src/test/java/org/x2b/study/core/graphql/Hello.java +++ /dev/null @@ -1,12 +0,0 @@ -package org.x2b.study.core.graphql; - -public class Hello { - - public Hello() { - - } - - public String getValue() { - return "this class is for testing schema creation but does not contain tests"; - } -} From 6a729c7aa04fb1459dc422a41db5991b0aa7e7a7 Mon Sep 17 00:00:00 2001 From: Ethan Hunter Date: Thu, 5 Oct 2017 16:14:39 -0600 Subject: [PATCH 05/47] refactor gradle build. still not quite working right. --- .gitignore | 4 +- README.md | 4 +- build.gradle | 40 ++-- core-service-lib-integration/build.gradle | 14 ++ core-service-lib/build.gradle | 24 +++ .../gradle/wrapper/gradle-wrapper.properties | 6 + core-service-lib/gradlew | 172 ++++++++++++++++++ core-service-lib/gradlew.bat | 84 +++++++++ .../study/core/GraphQLServiceConfigure.java | 3 - .../graphql/errors/UnauthorizedException.java | 0 .../org/x2b/study/core/security/User.java | 0 .../study/core/security/data/Permission.java | 0 .../security/data/PermissionRepository.java | 0 .../data/mongodb/AuthenticatedUser.java | 0 .../data/mongodb/AuthorizationRepository.java | 0 gradle/wrapper/gradle-wrapper.jar | Bin 54708 -> 54712 bytes gradle/wrapper/gradle-wrapper.properties | 1 + settings.gradle | 4 +- src/main/resources/application.yaml | 16 -- src/main/resources/schema.gql | 0 .../core/TestGraphQLServiceConfigure.java | 60 ------ src/test/resources/schema.gql | 17 -- 22 files changed, 322 insertions(+), 127 deletions(-) create mode 100644 core-service-lib-integration/build.gradle create mode 100644 core-service-lib/build.gradle create mode 100644 core-service-lib/gradle/wrapper/gradle-wrapper.properties create mode 100755 core-service-lib/gradlew create mode 100644 core-service-lib/gradlew.bat rename {src => core-service-lib/src}/main/java/org/x2b/study/core/GraphQLServiceConfigure.java (93%) rename {src => core-service-lib/src}/main/java/org/x2b/study/core/graphql/errors/UnauthorizedException.java (100%) rename {src => core-service-lib/src}/main/java/org/x2b/study/core/security/User.java (100%) rename {src => core-service-lib/src}/main/java/org/x2b/study/core/security/data/Permission.java (100%) rename {src => core-service-lib/src}/main/java/org/x2b/study/core/security/data/PermissionRepository.java (100%) rename {src => core-service-lib/src}/main/java/org/x2b/study/core/security/data/mongodb/AuthenticatedUser.java (100%) rename {src => core-service-lib/src}/main/java/org/x2b/study/core/security/data/mongodb/AuthorizationRepository.java (100%) delete mode 100644 src/main/resources/application.yaml delete mode 100644 src/main/resources/schema.gql delete mode 100644 src/test/java/org/x2b/study/core/TestGraphQLServiceConfigure.java delete mode 100644 src/test/resources/schema.gql diff --git a/.gitignore b/.gitignore index 8fd5bd8..95940fc 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,7 @@ # Created by .ignore support plugin (hsz.mobi) ### Gradle template .gradle -/build/ +build/ # Ignore Gradle GUI config gradle-app.setting @@ -37,6 +37,6 @@ gradle-app.setting # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml hs_err_pid* -/out +out/ .idea *.iml diff --git a/README.md b/README.md index 7647456..8ea8ed9 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ -![build status image](https://travis-ci.org/Team0x2B/core-service-lib.svg?branch=master) +![build status image](https://travis-ci.org/Team0x2B/org.x2b.study.core-service-lib.svg?branch=master) Right now this is just me messing around with GraphQL and Spring until I find a pattern I like. Eventually this will be -the core service library used to start writing a new backend service. +the org.x2b.study.core service library used to start writing a new backend service. # GraphQL Conventions diff --git a/build.gradle b/build.gradle index ab3068d..854ef2d 100644 --- a/build.gradle +++ b/build.gradle @@ -1,43 +1,31 @@ -group 'org.x2b.study.core' +group 'org.x2b.study' version '0.1-SNAPSHOT' + +tasks { + task wrapper(type: Wrapper) { + gradleVersion = '4.2' + } +} + buildscript { repositories { maven { url "https://plugins.gradle.org/m2/" } maven { url 'http://repo.spring.io/plugins-release' } - maven {url 'https://repo.spring.io/libs-release'} + } dependencies { - classpath("org.springframework.boot:spring-boot-gradle-plugin:1.5.7.RELEASE") + classpath "org.springframework.boot:spring-boot-gradle-plugin:1.5.7.RELEASE" classpath "com.jfrog.bintray.gradle:gradle-bintray-plugin:1.6" } } -tasks { - task wrapper(type: Wrapper) { - gradleVersion = '4.2' - } -} - apply plugin: 'java' -apply plugin: 'org.springframework.boot' - -sourceCompatibility = 1.8 -targetCompatibility = 1.8 - -repositories { - mavenCentral() +subprojects { + apply plugin: 'java' } -bootRepackage.enabled = false dependencies { - testCompile group: 'junit', name: 'junit', version: '4.12' - - compile 'org.springframework.boot:spring-boot-starter-web' - - compile 'com.graphql-java:graphql-spring-boot-starter:3.9.2' - - compile 'com.graphql-java:graphiql-spring-boot-starter:3.9.2' - - compile 'org.springframework.data:spring-data-mongodb:2.0.0.RELEASE' + compile project(":core-service-lib") + compile project(":core-service-lib-integration") } diff --git a/core-service-lib-integration/build.gradle b/core-service-lib-integration/build.gradle new file mode 100644 index 0000000..d0664b1 --- /dev/null +++ b/core-service-lib-integration/build.gradle @@ -0,0 +1,14 @@ +group 'org.x2b.study.org.x2b.study.core' +version '0.1-SNAPSHOT' + +apply plugin: 'java' + +sourceCompatibility = 1.8 + +repositories { + mavenCentral() +} + +dependencies { + testCompile group: 'junit', name: 'junit', version: '4.12' +} diff --git a/core-service-lib/build.gradle b/core-service-lib/build.gradle new file mode 100644 index 0000000..bc66dfa --- /dev/null +++ b/core-service-lib/build.gradle @@ -0,0 +1,24 @@ +group 'org.x2b.study.org.x2b.study.core' +version '0.1-SNAPSHOT' + + + +apply plugin: 'java' +apply plugin: 'org.springframework.boot' + +sourceCompatibility = 1.8 +targetCompatibility = 1.8 + +bootRepackage.enabled = false + +dependencies { + testCompile group: 'junit', name: 'junit', version: '4.12' + + compile 'org.springframework.boot:spring-boot-starter-web' + + compile 'com.graphql-java:graphql-spring-boot-starter:3.9.2' + + compile 'com.graphql-java:graphiql-spring-boot-starter:3.9.2' + + compile 'org.springframework.data:spring-data-mongodb:2.0.0.RELEASE' +} diff --git a/core-service-lib/gradle/wrapper/gradle-wrapper.properties b/core-service-lib/gradle/wrapper/gradle-wrapper.properties new file mode 100644 index 0000000..807d49e --- /dev/null +++ b/core-service-lib/gradle/wrapper/gradle-wrapper.properties @@ -0,0 +1,6 @@ +#Thu Oct 05 15:56:37 MDT 2017 +distributionBase=GRADLE_USER_HOME +distributionPath=wrapper/dists +zipStoreBase=GRADLE_USER_HOME +zipStorePath=wrapper/dists +distributionUrl=https\://services.gradle.org/distributions/gradle-4.0.1-bin.zip diff --git a/core-service-lib/gradlew b/core-service-lib/gradlew new file mode 100755 index 0000000..cccdd3d --- /dev/null +++ b/core-service-lib/gradlew @@ -0,0 +1,172 @@ +#!/usr/bin/env sh + +############################################################################## +## +## Gradle start up script for UN*X +## +############################################################################## + +# Attempt to set APP_HOME +# Resolve links: $0 may be a link +PRG="$0" +# Need this for relative symlinks. +while [ -h "$PRG" ] ; do + ls=`ls -ld "$PRG"` + link=`expr "$ls" : '.*-> \(.*\)$'` + if expr "$link" : '/.*' > /dev/null; then + PRG="$link" + else + PRG=`dirname "$PRG"`"/$link" + fi +done +SAVED="`pwd`" +cd "`dirname \"$PRG\"`/" >/dev/null +APP_HOME="`pwd -P`" +cd "$SAVED" >/dev/null + +APP_NAME="Gradle" +APP_BASE_NAME=`basename "$0"` + +# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +DEFAULT_JVM_OPTS="" + +# Use the maximum available, or set MAX_FD != -1 to use that value. +MAX_FD="maximum" + +warn () { + echo "$*" +} + +die () { + echo + echo "$*" + echo + exit 1 +} + +# OS specific support (must be 'true' or 'false'). +cygwin=false +msys=false +darwin=false +nonstop=false +case "`uname`" in + CYGWIN* ) + cygwin=true + ;; + Darwin* ) + darwin=true + ;; + MINGW* ) + msys=true + ;; + NONSTOP* ) + nonstop=true + ;; +esac + +CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar + +# Determine the Java command to use to start the JVM. +if [ -n "$JAVA_HOME" ] ; then + if [ -x "$JAVA_HOME/jre/sh/java" ] ; then + # IBM's JDK on AIX uses strange locations for the executables + JAVACMD="$JAVA_HOME/jre/sh/java" + else + JAVACMD="$JAVA_HOME/bin/java" + fi + if [ ! -x "$JAVACMD" ] ; then + die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." + fi +else + JAVACMD="java" + which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. + +Please set the JAVA_HOME variable in your environment to match the +location of your Java installation." +fi + +# Increase the maximum file descriptors if we can. +if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then + MAX_FD_LIMIT=`ulimit -H -n` + if [ $? -eq 0 ] ; then + if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then + MAX_FD="$MAX_FD_LIMIT" + fi + ulimit -n $MAX_FD + if [ $? -ne 0 ] ; then + warn "Could not set maximum file descriptor limit: $MAX_FD" + fi + else + warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" + fi +fi + +# For Darwin, add options to specify how the application appears in the dock +if $darwin; then + GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" +fi + +# For Cygwin, switch paths to Windows format before running java +if $cygwin ; then + APP_HOME=`cygpath --path --mixed "$APP_HOME"` + CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` + JAVACMD=`cygpath --unix "$JAVACMD"` + + # We build the pattern for arguments to be converted via cygpath + ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` + SEP="" + for dir in $ROOTDIRSRAW ; do + ROOTDIRS="$ROOTDIRS$SEP$dir" + SEP="|" + done + OURCYGPATTERN="(^($ROOTDIRS))" + # Add a user-defined pattern to the cygpath arguments + if [ "$GRADLE_CYGPATTERN" != "" ] ; then + OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" + fi + # Now convert the arguments - kludge to limit ourselves to /bin/sh + i=0 + for arg in "$@" ; do + CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` + CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option + + if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition + eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` + else + eval `echo args$i`="\"$arg\"" + fi + i=$((i+1)) + done + case $i in + (0) set -- ;; + (1) set -- "$args0" ;; + (2) set -- "$args0" "$args1" ;; + (3) set -- "$args0" "$args1" "$args2" ;; + (4) set -- "$args0" "$args1" "$args2" "$args3" ;; + (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; + (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; + (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; + (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; + (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; + esac +fi + +# Escape application args +save () { + for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done + echo " " +} +APP_ARGS=$(save "$@") + +# Collect all arguments for the java command, following the shell quoting and substitution rules +eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" + +# by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong +if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then + cd "$(dirname "$0")" +fi + +exec "$JAVACMD" "$@" diff --git a/core-service-lib/gradlew.bat b/core-service-lib/gradlew.bat new file mode 100644 index 0000000..e95643d --- /dev/null +++ b/core-service-lib/gradlew.bat @@ -0,0 +1,84 @@ +@if "%DEBUG%" == "" @echo off +@rem ########################################################################## +@rem +@rem Gradle startup script for Windows +@rem +@rem ########################################################################## + +@rem Set local scope for the variables with windows NT shell +if "%OS%"=="Windows_NT" setlocal + +set DIRNAME=%~dp0 +if "%DIRNAME%" == "" set DIRNAME=. +set APP_BASE_NAME=%~n0 +set APP_HOME=%DIRNAME% + +@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. +set DEFAULT_JVM_OPTS= + +@rem Find java.exe +if defined JAVA_HOME goto findJavaFromJavaHome + +set JAVA_EXE=java.exe +%JAVA_EXE% -version >NUL 2>&1 +if "%ERRORLEVEL%" == "0" goto init + +echo. +echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:findJavaFromJavaHome +set JAVA_HOME=%JAVA_HOME:"=% +set JAVA_EXE=%JAVA_HOME%/bin/java.exe + +if exist "%JAVA_EXE%" goto init + +echo. +echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% +echo. +echo Please set the JAVA_HOME variable in your environment to match the +echo location of your Java installation. + +goto fail + +:init +@rem Get command-line arguments, handling Windows variants + +if not "%OS%" == "Windows_NT" goto win9xME_args + +:win9xME_args +@rem Slurp the command line arguments. +set CMD_LINE_ARGS= +set _SKIP=2 + +:win9xME_args_slurp +if "x%~1" == "x" goto execute + +set CMD_LINE_ARGS=%* + +:execute +@rem Setup the command line + +set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar + +@rem Execute Gradle +"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% + +:end +@rem End local scope for the variables with windows NT shell +if "%ERRORLEVEL%"=="0" goto mainEnd + +:fail +rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of +rem the _cmd.exe /c_ return code! +if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 +exit /b 1 + +:mainEnd +if "%OS%"=="Windows_NT" endlocal + +:omega diff --git a/src/main/java/org/x2b/study/core/GraphQLServiceConfigure.java b/core-service-lib/src/main/java/org/x2b/study/core/GraphQLServiceConfigure.java similarity index 93% rename from src/main/java/org/x2b/study/core/GraphQLServiceConfigure.java rename to core-service-lib/src/main/java/org/x2b/study/core/GraphQLServiceConfigure.java index 9be8488..4871de1 100644 --- a/src/main/java/org/x2b/study/core/GraphQLServiceConfigure.java +++ b/core-service-lib/src/main/java/org/x2b/study/core/GraphQLServiceConfigure.java @@ -1,14 +1,11 @@ package org.x2b.study.core; -import graphql.GraphQL; import graphql.schema.GraphQLSchema; - import graphql.schema.idl.RuntimeWiring; import graphql.schema.idl.SchemaGenerator; import graphql.schema.idl.TypeDefinitionRegistry; import org.springframework.beans.factory.annotation.Value; -import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; diff --git a/src/main/java/org/x2b/study/core/graphql/errors/UnauthorizedException.java b/core-service-lib/src/main/java/org/x2b/study/core/graphql/errors/UnauthorizedException.java similarity index 100% rename from src/main/java/org/x2b/study/core/graphql/errors/UnauthorizedException.java rename to core-service-lib/src/main/java/org/x2b/study/core/graphql/errors/UnauthorizedException.java diff --git a/src/main/java/org/x2b/study/core/security/User.java b/core-service-lib/src/main/java/org/x2b/study/core/security/User.java similarity index 100% rename from src/main/java/org/x2b/study/core/security/User.java rename to core-service-lib/src/main/java/org/x2b/study/core/security/User.java diff --git a/src/main/java/org/x2b/study/core/security/data/Permission.java b/core-service-lib/src/main/java/org/x2b/study/core/security/data/Permission.java similarity index 100% rename from src/main/java/org/x2b/study/core/security/data/Permission.java rename to core-service-lib/src/main/java/org/x2b/study/core/security/data/Permission.java diff --git a/src/main/java/org/x2b/study/core/security/data/PermissionRepository.java b/core-service-lib/src/main/java/org/x2b/study/core/security/data/PermissionRepository.java similarity index 100% rename from src/main/java/org/x2b/study/core/security/data/PermissionRepository.java rename to core-service-lib/src/main/java/org/x2b/study/core/security/data/PermissionRepository.java diff --git a/src/main/java/org/x2b/study/core/security/data/mongodb/AuthenticatedUser.java b/core-service-lib/src/main/java/org/x2b/study/core/security/data/mongodb/AuthenticatedUser.java similarity index 100% rename from src/main/java/org/x2b/study/core/security/data/mongodb/AuthenticatedUser.java rename to core-service-lib/src/main/java/org/x2b/study/core/security/data/mongodb/AuthenticatedUser.java diff --git a/src/main/java/org/x2b/study/core/security/data/mongodb/AuthorizationRepository.java b/core-service-lib/src/main/java/org/x2b/study/core/security/data/mongodb/AuthorizationRepository.java similarity index 100% rename from src/main/java/org/x2b/study/core/security/data/mongodb/AuthorizationRepository.java rename to core-service-lib/src/main/java/org/x2b/study/core/security/data/mongodb/AuthorizationRepository.java diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar index 736fb7d3f94c051b359fc7ae7212d351bc094bdd..b35a838eb4f89cc90462ea4c5714213c9f20b52a 100644 GIT binary patch delta 808 zcmYk4Ur5tY6vuz}IOeuC-Ky#QHCrjd_7g1&%W|Ps(r76Qt!6~^pcztGf<6dR(u0(u z=9jWek$mVy2!shjfyy?m%{Bkb24=%Kr-=I+z?dp_r!d+)i|H<~mtn$&w( z`!T1pxlEFz^N&iaH7glEn%0__)GLqqoPL%2Ub!2;{6|dl2UU{CovfLj_MA2ICs3ssMQjy!)M=;^6L1CmY6E_%c)AnY&KW8+g%T#fP~vs=FoR-65t)LG!gfUt(eq|5v+*@j zKpiby9>azA3T3u(X~&gT117jNwTYz4wq4>@2cj~aYZsxu_8it=4s4;8ERT86f!>8;GWQGV?bnO8s)58|{dp!}@jx2iK%vD! z(KdC+#QQ1a7#7yLND zYF6R3)a)6y3j5qeN<9|SKFl@H&Zh!P(g(>m%+ZQl>1rn9UE0GIVVLh=KXGaPJbfI; lm+=PvK$V5y$W#`u{i**Si7(T%u~1Ima5Gb(X^O0we*sNw0^a}t diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 52dd1f0..1d414f4 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,3 +1,4 @@ +#Thu Oct 05 16:02:22 MDT 2017 distributionBase=GRADLE_USER_HOME distributionPath=wrapper/dists zipStoreBase=GRADLE_USER_HOME diff --git a/settings.gradle b/settings.gradle index efd0dfe..ccb39a7 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1,2 +1,4 @@ -rootProject.name = 'core-service-lib' +rootProject.name = 'core-service' +include 'core-service-lib' +include 'core-service-lib-integration' diff --git a/src/main/resources/application.yaml b/src/main/resources/application.yaml deleted file mode 100644 index d9f29c6..0000000 --- a/src/main/resources/application.yaml +++ /dev/null @@ -1,16 +0,0 @@ -server: - port: 5000 - -logging: - file: server_log.log - -graphql: - servlet: - mapping: /graphql - enabled: true - corsEnabled: true - -graphiql: - mapping: /graphiql - endpoint: /graphql - enabled: true diff --git a/src/main/resources/schema.gql b/src/main/resources/schema.gql deleted file mode 100644 index e69de29..0000000 diff --git a/src/test/java/org/x2b/study/core/TestGraphQLServiceConfigure.java b/src/test/java/org/x2b/study/core/TestGraphQLServiceConfigure.java deleted file mode 100644 index 4439874..0000000 --- a/src/test/java/org/x2b/study/core/TestGraphQLServiceConfigure.java +++ /dev/null @@ -1,60 +0,0 @@ -package org.x2b.study.core; - -import graphql.schema.GraphQLSchema; -import graphql.schema.GraphQLType; -import graphql.schema.idl.RuntimeWiring; -import org.junit.Assert; -import org.junit.Test; - -import java.util.List; - -public class TestGraphQLServiceConfigure { - - - - private class TestConfigure extends GraphQLServiceConfigure { - public class Hello { - - public Hello() { - - } - - public String getValue() { - return "this class is for testing schema creation but does not contain tests"; - } - } - - @Override - protected RuntimeWiring createRuntimeWiring() { - return RuntimeWiring.newRuntimeWiring() - .type("QueryRoot", w -> w - .dataFetcher("getHello", environment -> { - return new Hello(); - }) - ) - .type("MutationRoot", w -> w - .dataFetcher("logAString", env -> "mutation") - ) - .build(); - } - } - - - @Test - public void testCreateSchema() { - TestConfigure configure = new TestConfigure(); - GraphQLSchema schema = configure.schema(); - Assert.assertNotNull(schema); - - GraphQLType queryRoot = schema.getQueryType(); - Assert.assertNotNull(queryRoot); - Assert.assertEquals("QueryRoot", queryRoot.getName()); - - GraphQLType mutRoot = schema.getMutationType(); - Assert.assertNotNull(mutRoot); - Assert.assertEquals("MutationRoot", mutRoot.getName()); - - List types = schema.getAllTypesAsList(); - Assert.assertEquals(types.toString(), 3 + 10, types.size()); //10 built in - } -} diff --git a/src/test/resources/schema.gql b/src/test/resources/schema.gql deleted file mode 100644 index da8688a..0000000 --- a/src/test/resources/schema.gql +++ /dev/null @@ -1,17 +0,0 @@ -type Hello { - value: String! -} - -type QueryRoot { - getHello: Hello! -} - -type MutationRoot { - logAString(str: String!): String! -} - - -schema { - query: QueryRoot - mutation: MutationRoot -} \ No newline at end of file From fea929ac7111d65104c6a43941bbfc9624fb612c Mon Sep 17 00:00:00 2001 From: Ethan Hunter Date: Thu, 5 Oct 2017 16:16:47 -0600 Subject: [PATCH 06/47] fix gitignore --- .gitignore | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.gitignore b/.gitignore index 95940fc..419eafd 100644 --- a/.gitignore +++ b/.gitignore @@ -1,7 +1,7 @@ # Created by .ignore support plugin (hsz.mobi) ### Gradle template .gradle -build/ +build # Ignore Gradle GUI config gradle-app.setting From c639b7d7ee51c168b8700616c0d2b72ae7bad145 Mon Sep 17 00:00:00 2001 From: Ethan Hunter Date: Thu, 5 Oct 2017 16:30:54 -0600 Subject: [PATCH 07/47] Fix gradle build in theory. --- .gitignore | 1 - build.gradle | 12 +++++++++--- core-service-lib/build.gradle | 4 +--- gradle/wrapper/gradle-wrapper.jar | Bin 54712 -> 54708 bytes gradle/wrapper/gradle-wrapper.properties | 1 - 5 files changed, 10 insertions(+), 8 deletions(-) diff --git a/.gitignore b/.gitignore index 419eafd..d424146 100644 --- a/.gitignore +++ b/.gitignore @@ -2,7 +2,6 @@ ### Gradle template .gradle build - # Ignore Gradle GUI config gradle-app.setting diff --git a/build.gradle b/build.gradle index 854ef2d..a5a7f42 100644 --- a/build.gradle +++ b/build.gradle @@ -15,16 +15,22 @@ buildscript { } dependencies { - classpath "org.springframework.boot:spring-boot-gradle-plugin:1.5.7.RELEASE" + classpath "org.springframework.boot:spring-boot-gradle-plugin:1.5.2.RELEASE" classpath "com.jfrog.bintray.gradle:gradle-bintray-plugin:1.6" } } -apply plugin: 'java' -subprojects { +allprojects { apply plugin: 'java' } +subprojects { + repositories { + mavenCentral() + maven {url 'https://repo.spring.io/libs-release'} + } +} + dependencies { compile project(":core-service-lib") compile project(":core-service-lib-integration") diff --git a/core-service-lib/build.gradle b/core-service-lib/build.gradle index bc66dfa..0a64eb1 100644 --- a/core-service-lib/build.gradle +++ b/core-service-lib/build.gradle @@ -2,8 +2,6 @@ group 'org.x2b.study.org.x2b.study.core' version '0.1-SNAPSHOT' - -apply plugin: 'java' apply plugin: 'org.springframework.boot' sourceCompatibility = 1.8 @@ -14,7 +12,7 @@ bootRepackage.enabled = false dependencies { testCompile group: 'junit', name: 'junit', version: '4.12' - compile 'org.springframework.boot:spring-boot-starter-web' + compile 'org.springframework.boot:spring-boot-starter-web:1.5.7.RELEASE' compile 'com.graphql-java:graphql-spring-boot-starter:3.9.2' diff --git a/gradle/wrapper/gradle-wrapper.jar b/gradle/wrapper/gradle-wrapper.jar index b35a838eb4f89cc90462ea4c5714213c9f20b52a..736fb7d3f94c051b359fc7ae7212d351bc094bdd 100644 GIT binary patch delta 673 zcmYL{T}YEr9LC@CH#Q^3-Zhl%ouaHVeP!wlOQ+ND zYF6R3)a)6y3j5qeN<9|SKFl@H&Zh!P(g(>m%+ZQl>1rn9UE0GIVVLh=KXGaPJbfI; lm+=PvK$V5y$W#`u{i**Si7(T%u~1Ima5Gb(X^O0we*sNw0^a}t delta 808 zcmYk4Ur5tY6vuz}IOeuC-Ky#QHCrjd_7g1&%W|Ps(r76Qt!6~^pcztGf<6dR(u0(u z=9jWek$mVy2!shjfyy?m%{Bkb24=%Kr-=I+z?dp_r!d+)i|H<~mtn$&w( z`!T1pxlEFz^N&iaH7glEn%0__)GLqqoPL%2Ub!2;{6|dl2UU{CovfLj_MA2ICs3ssMQjy!)M=;^6L1CmY6E_%c)AnY&KW8+g%T#fP~vs=FoR-65t)LG!gfUt(eq|5v+*@j zKpiby9>azA3T3u(X~&gT117jNwTYz4wq4>@2cj~aYZsxu_8it=4s4;8ERT86f!>8;GWQGV?bnO8s)58|{dp!}@jx2iK%vD! z(KdC+#QQ1a7#7yL Date: Thu, 5 Oct 2017 16:36:54 -0600 Subject: [PATCH 08/47] add config and tests that somehow got lost in refactor --- .../src/main/resources/application.yaml | 16 ++++++ .../core/TestGraphQLServiceConfigure.java | 57 +++++++++++++++++++ .../src/test/resources/schema.gql | 17 ++++++ 3 files changed, 90 insertions(+) create mode 100644 core-service-lib/src/main/resources/application.yaml create mode 100644 core-service-lib/src/test/java/org/x2b/study/core/TestGraphQLServiceConfigure.java create mode 100644 core-service-lib/src/test/resources/schema.gql diff --git a/core-service-lib/src/main/resources/application.yaml b/core-service-lib/src/main/resources/application.yaml new file mode 100644 index 0000000..f533c5e --- /dev/null +++ b/core-service-lib/src/main/resources/application.yaml @@ -0,0 +1,16 @@ +server: + port: 5000 + +logging: + file: server_log.log + +graphql: + servlet: + mapping: /graphql + enabled: true + corsEnabled: true + +graphiql: + mapping: /graphiql + endpoint: /graphql + enabled: true \ No newline at end of file diff --git a/core-service-lib/src/test/java/org/x2b/study/core/TestGraphQLServiceConfigure.java b/core-service-lib/src/test/java/org/x2b/study/core/TestGraphQLServiceConfigure.java new file mode 100644 index 0000000..bba56f9 --- /dev/null +++ b/core-service-lib/src/test/java/org/x2b/study/core/TestGraphQLServiceConfigure.java @@ -0,0 +1,57 @@ +package org.x2b.study.core; + +import graphql.schema.GraphQLSchema; +import graphql.schema.GraphQLType; +import graphql.schema.idl.RuntimeWiring; +import org.junit.Assert; +import org.junit.Test; + +import java.util.List; + +public class TestGraphQLServiceConfigure { + private class TestConfigure extends GraphQLServiceConfigure { + public class Hello { + + public Hello() { + + } + + public String getValue() { + return "this class is for testing schema creation but does not contain tests"; + } + } + + @Override + protected RuntimeWiring createRuntimeWiring() { + return RuntimeWiring.newRuntimeWiring() + .type("QueryRoot", w -> w + .dataFetcher("getHello", environment -> { + return new Hello(); + }) + ) + .type("MutationRoot", w -> w + .dataFetcher("logAString", env -> "mutation") + ) + .build(); + } + } + + + @Test + public void testCreateSchema() { + TestConfigure configure = new TestConfigure(); + GraphQLSchema schema = configure.schema(); + Assert.assertNotNull(schema); + + GraphQLType queryRoot = schema.getQueryType(); + Assert.assertNotNull(queryRoot); + Assert.assertEquals("QueryRoot", queryRoot.getName()); + + GraphQLType mutRoot = schema.getMutationType(); + Assert.assertNotNull(mutRoot); + Assert.assertEquals("MutationRoot", mutRoot.getName()); + + List types = schema.getAllTypesAsList(); + Assert.assertEquals(types.toString(), 3 + 10, types.size()); //10 built in + } +} diff --git a/core-service-lib/src/test/resources/schema.gql b/core-service-lib/src/test/resources/schema.gql new file mode 100644 index 0000000..da8688a --- /dev/null +++ b/core-service-lib/src/test/resources/schema.gql @@ -0,0 +1,17 @@ +type Hello { + value: String! +} + +type QueryRoot { + getHello: Hello! +} + +type MutationRoot { + logAString(str: String!): String! +} + + +schema { + query: QueryRoot + mutation: MutationRoot +} \ No newline at end of file From 378616c454eff8d373058cfc3d02516db4d9290c Mon Sep 17 00:00:00 2001 From: Ethan Hunter Date: Thu, 5 Oct 2017 16:39:48 -0600 Subject: [PATCH 09/47] test commit to ensure travis is running unit tests correctly --- .../java/org/x2b/study/core/TestGraphQLServiceConfigure.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/core-service-lib/src/test/java/org/x2b/study/core/TestGraphQLServiceConfigure.java b/core-service-lib/src/test/java/org/x2b/study/core/TestGraphQLServiceConfigure.java index bba56f9..0ec837e 100644 --- a/core-service-lib/src/test/java/org/x2b/study/core/TestGraphQLServiceConfigure.java +++ b/core-service-lib/src/test/java/org/x2b/study/core/TestGraphQLServiceConfigure.java @@ -53,5 +53,7 @@ public void testCreateSchema() { List types = schema.getAllTypesAsList(); Assert.assertEquals(types.toString(), 3 + 10, types.size()); //10 built in + + Assert.fail(); } } From b1064f625b2145361d5ea1de6848280c2159eec7 Mon Sep 17 00:00:00 2001 From: Ethan Hunter Date: Thu, 5 Oct 2017 16:44:25 -0600 Subject: [PATCH 10/47] update readme build link and fix test (travis seems to work) --- README.md | 2 +- .../java/org/x2b/study/core/TestGraphQLServiceConfigure.java | 2 -- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/README.md b/README.md index 8ea8ed9..c0b2fae 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -![build status image](https://travis-ci.org/Team0x2B/org.x2b.study.core-service-lib.svg?branch=master) +[![build status image](https://travis-ci.org/Team0x2B/org.x2b.study.core-service-lib.svg?branch=master)](https://travis-ci.org/Team0x2B/core-service-lib) Right now this is just me messing around with GraphQL and Spring until I find a pattern I like. Eventually this will be the org.x2b.study.core service library used to start writing a new backend service. diff --git a/core-service-lib/src/test/java/org/x2b/study/core/TestGraphQLServiceConfigure.java b/core-service-lib/src/test/java/org/x2b/study/core/TestGraphQLServiceConfigure.java index 0ec837e..bba56f9 100644 --- a/core-service-lib/src/test/java/org/x2b/study/core/TestGraphQLServiceConfigure.java +++ b/core-service-lib/src/test/java/org/x2b/study/core/TestGraphQLServiceConfigure.java @@ -53,7 +53,5 @@ public void testCreateSchema() { List types = schema.getAllTypesAsList(); Assert.assertEquals(types.toString(), 3 + 10, types.size()); //10 built in - - Assert.fail(); } } From bfa2f5838f37a16e8d2efd9d7f4443bed76ef6ac Mon Sep 17 00:00:00 2001 From: Ethan Hunter Date: Thu, 5 Oct 2017 16:48:38 -0600 Subject: [PATCH 11/47] tone down email notifications --- .travis.yml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/.travis.yml b/.travis.yml index 6a25a6d..ee5484e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,3 +5,8 @@ jdk: before_install: - chmod +x gradlew - chmod +x gradle/wrapper/gradle-wrapper.jar + +notifications: + email: + on_success: never + on_failure: always \ No newline at end of file From 7a2aa05e116e1860fd5c3b1fa2dd7a1005c45857 Mon Sep 17 00:00:00 2001 From: Ethan Hunter Date: Thu, 5 Oct 2017 17:06:37 -0600 Subject: [PATCH 12/47] add secure slack notification for travis --- .travis.yml | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/.travis.yml b/.travis.yml index ee5484e..dd26920 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,12 +1,12 @@ language: java - jdk: - - oraclejdk8 +- oraclejdk8 before_install: - - chmod +x gradlew - - chmod +x gradle/wrapper/gradle-wrapper.jar - +- chmod +x gradlew +- chmod +x gradle/wrapper/gradle-wrapper.jar notifications: email: on_success: never - on_failure: always \ No newline at end of file + on_failure: always + slack: + secure: zbrlGnn3XROnnOnu3QIKb+LHTkGbJrqW3xyUNE/egHZ2F6214IrW3nMQ1UCWcf3BgrcA/t+ATPUGT0DJBUbzRypQrJadkG82KavGllAhd6KUBSKGFEGq4+6ug8g+1OhOs/EyxbMH98dftPO9IABs0bByHARU8EQEvaRTkX+MvaF0I6Ni/3nFoYn2xf6aSEZ8/S20co59BN+1GjLTippJERTREWsj6A5LDlsyH5Dw9REbhp2UXRbR2zlw2hbQu6HxTLSf8FAarEh0NsXI5EeCkO2TJsL1R0CejFznPRIk2WLr4TebgWY6Hdk3EUlwuM/4JqEqgml0yLg8/BpG3oB/iXJaYZOhRW7ZALJ9KFCD2CKI4Kdi1CeryGtabl6FfQene9vPDxGkmolTOFtglW5MoHIpLVE/oni0GTQEaqHhdYqCBJ/gHLx+Mrc433cDgIEd9RNqCa6vNzmfS7h7+yHKH4OxzK+PCDezsTvrwca1VCMhKBtdT9YI+hOys5J76Gf42zW8EpY9BsC05H3g+Pp0pbVIe6rmu5RShgJUne16LlkrsLKr03mZes0XsYOYA7VgnMMLFMwd/urbtcKOe1YClfKiAuBcFrpdEbSS8boqkxLbG0BmfcyXyf35eFhKFghom0dZNLa+BgMQ0os7vk3IE25f46/rzmpZRmAUlsW7aco= From 0cbd4f05a332acd09d9607a78f929e7322e8a1ae Mon Sep 17 00:00:00 2001 From: Ethan Hunter Date: Thu, 5 Oct 2017 17:07:42 -0600 Subject: [PATCH 13/47] fix travis slack notification settings --- .travis.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.travis.yml b/.travis.yml index dd26920..a6bea9f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,3 +10,5 @@ notifications: on_failure: always slack: secure: zbrlGnn3XROnnOnu3QIKb+LHTkGbJrqW3xyUNE/egHZ2F6214IrW3nMQ1UCWcf3BgrcA/t+ATPUGT0DJBUbzRypQrJadkG82KavGllAhd6KUBSKGFEGq4+6ug8g+1OhOs/EyxbMH98dftPO9IABs0bByHARU8EQEvaRTkX+MvaF0I6Ni/3nFoYn2xf6aSEZ8/S20co59BN+1GjLTippJERTREWsj6A5LDlsyH5Dw9REbhp2UXRbR2zlw2hbQu6HxTLSf8FAarEh0NsXI5EeCkO2TJsL1R0CejFznPRIk2WLr4TebgWY6Hdk3EUlwuM/4JqEqgml0yLg8/BpG3oB/iXJaYZOhRW7ZALJ9KFCD2CKI4Kdi1CeryGtabl6FfQene9vPDxGkmolTOFtglW5MoHIpLVE/oni0GTQEaqHhdYqCBJ/gHLx+Mrc433cDgIEd9RNqCa6vNzmfS7h7+yHKH4OxzK+PCDezsTvrwca1VCMhKBtdT9YI+hOys5J76Gf42zW8EpY9BsC05H3g+Pp0pbVIe6rmu5RShgJUne16LlkrsLKr03mZes0XsYOYA7VgnMMLFMwd/urbtcKOe1YClfKiAuBcFrpdEbSS8boqkxLbG0BmfcyXyf35eFhKFghom0dZNLa+BgMQ0os7vk3IE25f46/rzmpZRmAUlsW7aco= + on_success: always + on_failure: always \ No newline at end of file From 4fab3459ce377c3f719d0938a91edfa81db3c5f5 Mon Sep 17 00:00:00 2001 From: Ethan Hunter Date: Fri, 6 Oct 2017 00:33:18 -0600 Subject: [PATCH 14/47] break the build --- core-service-lib-integration/build.gradle | 2 ++ .../study/core/IntegrationTestService.java | 29 +++++++++++++++++++ .../fetchers/CreateEmptyUserFetcher.java | 12 ++++++++ .../src/main/resources/schema.gql | 12 ++++++++ .../study/core/GraphQLServiceConfigure.java | 14 ++++++--- .../data/mongodb/AuthenticatedUser.java | 8 ++--- 6 files changed, 69 insertions(+), 8 deletions(-) create mode 100644 core-service-lib-integration/src/main/java/org/x2b/study/core/IntegrationTestService.java create mode 100644 core-service-lib-integration/src/main/java/org/x2b/study/core/graphql/fetchers/CreateEmptyUserFetcher.java create mode 100644 core-service-lib-integration/src/main/resources/schema.gql diff --git a/core-service-lib-integration/build.gradle b/core-service-lib-integration/build.gradle index d0664b1..18748a7 100644 --- a/core-service-lib-integration/build.gradle +++ b/core-service-lib-integration/build.gradle @@ -11,4 +11,6 @@ repositories { dependencies { testCompile group: 'junit', name: 'junit', version: '4.12' + + compile project(':core-service-lib') } diff --git a/core-service-lib-integration/src/main/java/org/x2b/study/core/IntegrationTestService.java b/core-service-lib-integration/src/main/java/org/x2b/study/core/IntegrationTestService.java new file mode 100644 index 0000000..6bc04c2 --- /dev/null +++ b/core-service-lib-integration/src/main/java/org/x2b/study/core/IntegrationTestService.java @@ -0,0 +1,29 @@ +package org.x2b.study.core; + +import graphql.schema.idl.RuntimeWiring; +import org.springframework.boot.CommandLineRunner; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.x2b.study.core.graphql.fetchers.CreateEmptyUserFetcher; + +@SpringBootApplication +public class IntegrationTestService extends GraphQLServiceConfigure implements CommandLineRunner { + + + public static void main(String[] args) { + SpringApplication.run(IntegrationTestService.class, args); + } + + @Override + protected RuntimeWiring createRuntimeWiring() { + return RuntimeWiring.newRuntimeWiring() + .type("MutationRoot", w -> w + .dataFetcher("createEmptyUser", new CreateEmptyUserFetcher()) + ).build(); + } + + @Override + public void run(String... args) throws Exception { + + } +} diff --git a/core-service-lib-integration/src/main/java/org/x2b/study/core/graphql/fetchers/CreateEmptyUserFetcher.java b/core-service-lib-integration/src/main/java/org/x2b/study/core/graphql/fetchers/CreateEmptyUserFetcher.java new file mode 100644 index 0000000..357aeb2 --- /dev/null +++ b/core-service-lib-integration/src/main/java/org/x2b/study/core/graphql/fetchers/CreateEmptyUserFetcher.java @@ -0,0 +1,12 @@ +package org.x2b.study.core.graphql.fetchers; + +import graphql.schema.DataFetcher; +import graphql.schema.DataFetchingEnvironment; +import graphql.schema.GraphQLScalarType; + +public class CreateEmptyUserFetcher implements DataFetcher { + @Override + public String get(DataFetchingEnvironment environment) { + return "akjsdhakjsd"; + } +} diff --git a/core-service-lib-integration/src/main/resources/schema.gql b/core-service-lib-integration/src/main/resources/schema.gql new file mode 100644 index 0000000..98c5b54 --- /dev/null +++ b/core-service-lib-integration/src/main/resources/schema.gql @@ -0,0 +1,12 @@ +type MutationRoot { + createEmptyUser: ID! +} + +type QueryRoot { + getAStr: String! +} + +schema { + mutation: MutationRoot + query: QueryRoot +} \ No newline at end of file diff --git a/core-service-lib/src/main/java/org/x2b/study/core/GraphQLServiceConfigure.java b/core-service-lib/src/main/java/org/x2b/study/core/GraphQLServiceConfigure.java index 4871de1..9666c68 100644 --- a/core-service-lib/src/main/java/org/x2b/study/core/GraphQLServiceConfigure.java +++ b/core-service-lib/src/main/java/org/x2b/study/core/GraphQLServiceConfigure.java @@ -4,6 +4,7 @@ import graphql.schema.GraphQLSchema; import graphql.schema.idl.RuntimeWiring; import graphql.schema.idl.SchemaGenerator; +import graphql.schema.idl.SchemaParser; import graphql.schema.idl.TypeDefinitionRegistry; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.SpringBootApplication; @@ -11,7 +12,6 @@ import java.io.File; -@SpringBootApplication public abstract class GraphQLServiceConfigure { @Value("#{graphql.schema.schemaFileLocation}") @@ -20,10 +20,14 @@ public abstract class GraphQLServiceConfigure { @Bean public GraphQLSchema schema() { - graphql.schema.idl.SchemaParser parser = new graphql.schema.idl.SchemaParser(); + SchemaParser parser = new SchemaParser(); SchemaGenerator schemaGenerator = new SchemaGenerator(); TypeDefinitionRegistry tdr = parser.parse(getSchemaFile()); - return schemaGenerator.makeExecutableSchema(tdr, createRuntimeWiring()); + RuntimeWiring runtimeWiring = createRuntimeWiring(); + if (runtimeWiring == null) { + return null; + } + return schemaGenerator.makeExecutableSchema(tdr, runtimeWiring); } private File getSchemaFile() { @@ -31,5 +35,7 @@ private File getSchemaFile() { } - protected abstract RuntimeWiring createRuntimeWiring(); + protected RuntimeWiring createRuntimeWiring() { + return null; + } } diff --git a/core-service-lib/src/main/java/org/x2b/study/core/security/data/mongodb/AuthenticatedUser.java b/core-service-lib/src/main/java/org/x2b/study/core/security/data/mongodb/AuthenticatedUser.java index 308fe13..e9875f5 100644 --- a/core-service-lib/src/main/java/org/x2b/study/core/security/data/mongodb/AuthenticatedUser.java +++ b/core-service-lib/src/main/java/org/x2b/study/core/security/data/mongodb/AuthenticatedUser.java @@ -10,12 +10,12 @@ public class AuthenticatedUser implements User{ @Id - private final UUID uuid; + private final UUID id; private final Set permissions; public AuthenticatedUser(UUID uuid, Set permissions) { - this.uuid = uuid; + this.id = uuid; this.permissions = new HashSet<>(permissions); } @@ -25,11 +25,11 @@ public boolean hasPermission(String permission) { @Override public UUID getUUID() { - return uuid; + return id; } @Override public String toString() { - return String.format("User: %s", uuid); + return String.format("User: %s", id); } } From e0fb4a8ce8030960a391a375b6fc6887858c564f Mon Sep 17 00:00:00 2001 From: Ethan Hunter Date: Fri, 6 Oct 2017 15:19:32 -0600 Subject: [PATCH 15/47] Fix build. Clean up imports --- build.gradle | 5 ++- core-service-lib-integration/build.gradle | 3 +- .../study/core/IntegrationTestService.java | 18 ++++----- .../fetchers/CreateEmptyUserFetcher.java | 1 - .../study/core/GraphQLServiceConfigure.java | 1 - .../core/TestGraphQLServiceConfigure.java | 40 ++++++++----------- 6 files changed, 32 insertions(+), 36 deletions(-) diff --git a/build.gradle b/build.gradle index a5a7f42..905d377 100644 --- a/build.gradle +++ b/build.gradle @@ -22,12 +22,15 @@ buildscript { allprojects { apply plugin: 'java' + + sourceCompatibility = 1.8 + targetCompatibility = 1.8 } subprojects { repositories { mavenCentral() - maven {url 'https://repo.spring.io/libs-release'} + maven {url 'http://repo.spring.io/libs-release'} } } diff --git a/core-service-lib-integration/build.gradle b/core-service-lib-integration/build.gradle index 18748a7..67ca729 100644 --- a/core-service-lib-integration/build.gradle +++ b/core-service-lib-integration/build.gradle @@ -3,7 +3,7 @@ version '0.1-SNAPSHOT' apply plugin: 'java' -sourceCompatibility = 1.8 +apply plugin: 'org.springframework.boot' repositories { mavenCentral() @@ -13,4 +13,5 @@ dependencies { testCompile group: 'junit', name: 'junit', version: '4.12' compile project(':core-service-lib') + } diff --git a/core-service-lib-integration/src/main/java/org/x2b/study/core/IntegrationTestService.java b/core-service-lib-integration/src/main/java/org/x2b/study/core/IntegrationTestService.java index 6bc04c2..b321339 100644 --- a/core-service-lib-integration/src/main/java/org/x2b/study/core/IntegrationTestService.java +++ b/core-service-lib-integration/src/main/java/org/x2b/study/core/IntegrationTestService.java @@ -1,29 +1,29 @@ package org.x2b.study.core; +import graphql.schema.StaticDataFetcher; import graphql.schema.idl.RuntimeWiring; -import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.x2b.study.core.graphql.fetchers.CreateEmptyUserFetcher; @SpringBootApplication -public class IntegrationTestService extends GraphQLServiceConfigure implements CommandLineRunner { +public class IntegrationTestService extends GraphQLServiceConfigure { public static void main(String[] args) { SpringApplication.run(IntegrationTestService.class, args); } - @Override + + protected RuntimeWiring createRuntimeWiring() { return RuntimeWiring.newRuntimeWiring() .type("MutationRoot", w -> w .dataFetcher("createEmptyUser", new CreateEmptyUserFetcher()) - ).build(); - } - - @Override - public void run(String... args) throws Exception { - + ) + .type("QueryRoot", w -> w + .dataFetcher("getAStr", new StaticDataFetcher("foo")) + ) + .build(); } } diff --git a/core-service-lib-integration/src/main/java/org/x2b/study/core/graphql/fetchers/CreateEmptyUserFetcher.java b/core-service-lib-integration/src/main/java/org/x2b/study/core/graphql/fetchers/CreateEmptyUserFetcher.java index 357aeb2..63e9f34 100644 --- a/core-service-lib-integration/src/main/java/org/x2b/study/core/graphql/fetchers/CreateEmptyUserFetcher.java +++ b/core-service-lib-integration/src/main/java/org/x2b/study/core/graphql/fetchers/CreateEmptyUserFetcher.java @@ -2,7 +2,6 @@ import graphql.schema.DataFetcher; import graphql.schema.DataFetchingEnvironment; -import graphql.schema.GraphQLScalarType; public class CreateEmptyUserFetcher implements DataFetcher { @Override diff --git a/core-service-lib/src/main/java/org/x2b/study/core/GraphQLServiceConfigure.java b/core-service-lib/src/main/java/org/x2b/study/core/GraphQLServiceConfigure.java index 9666c68..f7bca87 100644 --- a/core-service-lib/src/main/java/org/x2b/study/core/GraphQLServiceConfigure.java +++ b/core-service-lib/src/main/java/org/x2b/study/core/GraphQLServiceConfigure.java @@ -7,7 +7,6 @@ import graphql.schema.idl.SchemaParser; import graphql.schema.idl.TypeDefinitionRegistry; import org.springframework.beans.factory.annotation.Value; -import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.context.annotation.Bean; import java.io.File; diff --git a/core-service-lib/src/test/java/org/x2b/study/core/TestGraphQLServiceConfigure.java b/core-service-lib/src/test/java/org/x2b/study/core/TestGraphQLServiceConfigure.java index bba56f9..63a99cf 100644 --- a/core-service-lib/src/test/java/org/x2b/study/core/TestGraphQLServiceConfigure.java +++ b/core-service-lib/src/test/java/org/x2b/study/core/TestGraphQLServiceConfigure.java @@ -1,12 +1,6 @@ package org.x2b.study.core; -import graphql.schema.GraphQLSchema; -import graphql.schema.GraphQLType; import graphql.schema.idl.RuntimeWiring; -import org.junit.Assert; -import org.junit.Test; - -import java.util.List; public class TestGraphQLServiceConfigure { private class TestConfigure extends GraphQLServiceConfigure { @@ -37,21 +31,21 @@ protected RuntimeWiring createRuntimeWiring() { } - @Test - public void testCreateSchema() { - TestConfigure configure = new TestConfigure(); - GraphQLSchema schema = configure.schema(); - Assert.assertNotNull(schema); - - GraphQLType queryRoot = schema.getQueryType(); - Assert.assertNotNull(queryRoot); - Assert.assertEquals("QueryRoot", queryRoot.getName()); - - GraphQLType mutRoot = schema.getMutationType(); - Assert.assertNotNull(mutRoot); - Assert.assertEquals("MutationRoot", mutRoot.getName()); - - List types = schema.getAllTypesAsList(); - Assert.assertEquals(types.toString(), 3 + 10, types.size()); //10 built in - } +// @Test +// public void testCreateSchema() { +// TestConfigure configure = new TestConfigure(); +// GraphQLSchema schema = configure.schema(); +// Assert.assertNotNull(schema); +// +// GraphQLType queryRoot = schema.getQueryType(); +// Assert.assertNotNull(queryRoot); +// Assert.assertEquals("QueryRoot", queryRoot.getName()); +// +// GraphQLType mutRoot = schema.getMutationType(); +// Assert.assertNotNull(mutRoot); +// Assert.assertEquals("MutationRoot", mutRoot.getName()); +// +// List types = schema.getAllTypesAsList(); +// Assert.assertEquals(types.toString(), 3 + 10, types.size()); //10 built in +// } } From 0f455262d6b86d8a37d4600371d41b90bcbc305e Mon Sep 17 00:00:00 2001 From: Ethan Hunter Date: Fri, 6 Oct 2017 15:21:20 -0600 Subject: [PATCH 16/47] fix unit tests --- .../core/TestGraphQLServiceConfigure.java | 40 +++++++++++-------- 1 file changed, 23 insertions(+), 17 deletions(-) diff --git a/core-service-lib/src/test/java/org/x2b/study/core/TestGraphQLServiceConfigure.java b/core-service-lib/src/test/java/org/x2b/study/core/TestGraphQLServiceConfigure.java index 63a99cf..bba56f9 100644 --- a/core-service-lib/src/test/java/org/x2b/study/core/TestGraphQLServiceConfigure.java +++ b/core-service-lib/src/test/java/org/x2b/study/core/TestGraphQLServiceConfigure.java @@ -1,6 +1,12 @@ package org.x2b.study.core; +import graphql.schema.GraphQLSchema; +import graphql.schema.GraphQLType; import graphql.schema.idl.RuntimeWiring; +import org.junit.Assert; +import org.junit.Test; + +import java.util.List; public class TestGraphQLServiceConfigure { private class TestConfigure extends GraphQLServiceConfigure { @@ -31,21 +37,21 @@ protected RuntimeWiring createRuntimeWiring() { } -// @Test -// public void testCreateSchema() { -// TestConfigure configure = new TestConfigure(); -// GraphQLSchema schema = configure.schema(); -// Assert.assertNotNull(schema); -// -// GraphQLType queryRoot = schema.getQueryType(); -// Assert.assertNotNull(queryRoot); -// Assert.assertEquals("QueryRoot", queryRoot.getName()); -// -// GraphQLType mutRoot = schema.getMutationType(); -// Assert.assertNotNull(mutRoot); -// Assert.assertEquals("MutationRoot", mutRoot.getName()); -// -// List types = schema.getAllTypesAsList(); -// Assert.assertEquals(types.toString(), 3 + 10, types.size()); //10 built in -// } + @Test + public void testCreateSchema() { + TestConfigure configure = new TestConfigure(); + GraphQLSchema schema = configure.schema(); + Assert.assertNotNull(schema); + + GraphQLType queryRoot = schema.getQueryType(); + Assert.assertNotNull(queryRoot); + Assert.assertEquals("QueryRoot", queryRoot.getName()); + + GraphQLType mutRoot = schema.getMutationType(); + Assert.assertNotNull(mutRoot); + Assert.assertEquals("MutationRoot", mutRoot.getName()); + + List types = schema.getAllTypesAsList(); + Assert.assertEquals(types.toString(), 3 + 10, types.size()); //10 built in + } } From e1fc6fa6ca3225d3418e5175befb06b83a0832ec Mon Sep 17 00:00:00 2001 From: Ethan Hunter Date: Fri, 6 Oct 2017 18:00:32 -0600 Subject: [PATCH 17/47] play around with mongo jpa layer --- .../study/core/IntegrationTestService.java | 12 +++++--- .../fetchers/CreateEmptyUserFetcher.java | 11 -------- .../createuser/CreateUserFetcher.java | 28 +++++++++++++++++++ .../query/getuser/GetUserFetcher.java | 27 ++++++++++++++++++ .../src/main/resources/schema.gql | 8 ++++-- .../data/mongodb/AuthenticatedUser.java | 8 ++++-- 6 files changed, 75 insertions(+), 19 deletions(-) delete mode 100644 core-service-lib-integration/src/main/java/org/x2b/study/core/graphql/fetchers/CreateEmptyUserFetcher.java create mode 100644 core-service-lib-integration/src/main/java/org/x2b/study/core/graphql/fetchers/mutation/createuser/CreateUserFetcher.java create mode 100644 core-service-lib-integration/src/main/java/org/x2b/study/core/graphql/fetchers/query/getuser/GetUserFetcher.java diff --git a/core-service-lib-integration/src/main/java/org/x2b/study/core/IntegrationTestService.java b/core-service-lib-integration/src/main/java/org/x2b/study/core/IntegrationTestService.java index b321339..40576f3 100644 --- a/core-service-lib-integration/src/main/java/org/x2b/study/core/IntegrationTestService.java +++ b/core-service-lib-integration/src/main/java/org/x2b/study/core/IntegrationTestService.java @@ -2,9 +2,12 @@ import graphql.schema.StaticDataFetcher; import graphql.schema.idl.RuntimeWiring; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.x2b.study.core.graphql.fetchers.CreateEmptyUserFetcher; +import org.x2b.study.core.graphql.fetchers.mutation.createuser.CreateUserFetcher; +import org.x2b.study.core.graphql.fetchers.query.getuser.GetUserFetcher; +import org.x2b.study.core.security.data.mongodb.AuthorizationRepository; @SpringBootApplication public class IntegrationTestService extends GraphQLServiceConfigure { @@ -14,15 +17,16 @@ public static void main(String[] args) { SpringApplication.run(IntegrationTestService.class, args); } - + @Autowired + public AuthorizationRepository authRepo; protected RuntimeWiring createRuntimeWiring() { return RuntimeWiring.newRuntimeWiring() .type("MutationRoot", w -> w - .dataFetcher("createEmptyUser", new CreateEmptyUserFetcher()) + .dataFetcher("createUser", new CreateUserFetcher(authRepo)) ) .type("QueryRoot", w -> w - .dataFetcher("getAStr", new StaticDataFetcher("foo")) + .dataFetcher("getUserPermissions", new GetUserFetcher(authRepo)) ) .build(); } diff --git a/core-service-lib-integration/src/main/java/org/x2b/study/core/graphql/fetchers/CreateEmptyUserFetcher.java b/core-service-lib-integration/src/main/java/org/x2b/study/core/graphql/fetchers/CreateEmptyUserFetcher.java deleted file mode 100644 index 63e9f34..0000000 --- a/core-service-lib-integration/src/main/java/org/x2b/study/core/graphql/fetchers/CreateEmptyUserFetcher.java +++ /dev/null @@ -1,11 +0,0 @@ -package org.x2b.study.core.graphql.fetchers; - -import graphql.schema.DataFetcher; -import graphql.schema.DataFetchingEnvironment; - -public class CreateEmptyUserFetcher implements DataFetcher { - @Override - public String get(DataFetchingEnvironment environment) { - return "akjsdhakjsd"; - } -} diff --git a/core-service-lib-integration/src/main/java/org/x2b/study/core/graphql/fetchers/mutation/createuser/CreateUserFetcher.java b/core-service-lib-integration/src/main/java/org/x2b/study/core/graphql/fetchers/mutation/createuser/CreateUserFetcher.java new file mode 100644 index 0000000..c0b5ddc --- /dev/null +++ b/core-service-lib-integration/src/main/java/org/x2b/study/core/graphql/fetchers/mutation/createuser/CreateUserFetcher.java @@ -0,0 +1,28 @@ +package org.x2b.study.core.graphql.fetchers.mutation.createuser; + +import graphql.schema.DataFetcher; +import graphql.schema.DataFetchingEnvironment; +import org.springframework.beans.factory.annotation.Autowired; +import org.x2b.study.core.security.data.mongodb.AuthenticatedUser; +import org.x2b.study.core.security.data.mongodb.AuthorizationRepository; + +import java.util.*; + +public class CreateUserFetcher implements DataFetcher { + + + private AuthorizationRepository authRepo; + + public CreateUserFetcher(AuthorizationRepository authRepo) { + this.authRepo = authRepo; + } + + @Override + public String get(DataFetchingEnvironment environment) { + HashMap input = environment.getArgument("input"); + List permissions = (List) input.get("permissions"); + AuthenticatedUser user = new AuthenticatedUser(UUID.randomUUID(), new HashSet<>(permissions)); + authRepo.save(user); + return user.getUUID().toString(); + } +} diff --git a/core-service-lib-integration/src/main/java/org/x2b/study/core/graphql/fetchers/query/getuser/GetUserFetcher.java b/core-service-lib-integration/src/main/java/org/x2b/study/core/graphql/fetchers/query/getuser/GetUserFetcher.java new file mode 100644 index 0000000..1e2d2d9 --- /dev/null +++ b/core-service-lib-integration/src/main/java/org/x2b/study/core/graphql/fetchers/query/getuser/GetUserFetcher.java @@ -0,0 +1,27 @@ +package org.x2b.study.core.graphql.fetchers.query.getuser; + +import graphql.schema.DataFetcher; +import graphql.schema.DataFetchingEnvironment; +import org.x2b.study.core.security.data.mongodb.AuthenticatedUser; +import org.x2b.study.core.security.data.mongodb.AuthorizationRepository; + +import java.util.ArrayList; +import java.util.List; +import java.util.UUID; + +public class GetUserFetcher implements DataFetcher> { + + private AuthorizationRepository authRepo; + + public GetUserFetcher(AuthorizationRepository authRepo) { + this.authRepo = authRepo; + } + + @Override + public List get(DataFetchingEnvironment environment) { + String idString = environment.getArgument("id"); + UUID uuid = UUID.fromString(idString); + AuthenticatedUser user = authRepo.findOne(uuid); + return new ArrayList<>(user.getPermissions()); + } +} diff --git a/core-service-lib-integration/src/main/resources/schema.gql b/core-service-lib-integration/src/main/resources/schema.gql index 98c5b54..47a2e1f 100644 --- a/core-service-lib-integration/src/main/resources/schema.gql +++ b/core-service-lib-integration/src/main/resources/schema.gql @@ -1,9 +1,13 @@ type MutationRoot { - createEmptyUser: ID! + createUser(input: PermissionsInput!): ID! +} + +input PermissionsInput { + permissions: [String!]! } type QueryRoot { - getAStr: String! + getUserPermissions(id: ID!): [String]! } schema { diff --git a/core-service-lib/src/main/java/org/x2b/study/core/security/data/mongodb/AuthenticatedUser.java b/core-service-lib/src/main/java/org/x2b/study/core/security/data/mongodb/AuthenticatedUser.java index e9875f5..785bda6 100644 --- a/core-service-lib/src/main/java/org/x2b/study/core/security/data/mongodb/AuthenticatedUser.java +++ b/core-service-lib/src/main/java/org/x2b/study/core/security/data/mongodb/AuthenticatedUser.java @@ -14,8 +14,8 @@ public class AuthenticatedUser implements User{ private final Set permissions; - public AuthenticatedUser(UUID uuid, Set permissions) { - this.id = uuid; + public AuthenticatedUser(UUID id, Set permissions) { + this.id = id; this.permissions = new HashSet<>(permissions); } @@ -23,6 +23,10 @@ public boolean hasPermission(String permission) { return permission.contains(permission); } + public Set getPermissions() { + return permissions; + } + @Override public UUID getUUID() { return id; From 581c8ce142664f649286adc2da5da05cad1e183c Mon Sep 17 00:00:00 2001 From: Ethan Hunter Date: Sat, 7 Oct 2017 18:30:20 -0600 Subject: [PATCH 18/47] add very basic integration tests --- core-service-lib-integration/build.gradle | 3 +- .../study/core/IntegrationTestService.java | 1 - .../createuser/CreateUserFetcher.java | 6 +- .../studi/core/TestIntegrationService.java | 57 +++++++++++++++++++ .../src/test/resources/schema.gql | 16 ++++++ 5 files changed, 79 insertions(+), 4 deletions(-) create mode 100644 core-service-lib-integration/src/test/java/org/x2b/studi/core/TestIntegrationService.java create mode 100644 core-service-lib-integration/src/test/resources/schema.gql diff --git a/core-service-lib-integration/build.gradle b/core-service-lib-integration/build.gradle index 67ca729..a1f5c12 100644 --- a/core-service-lib-integration/build.gradle +++ b/core-service-lib-integration/build.gradle @@ -12,6 +12,7 @@ repositories { dependencies { testCompile group: 'junit', name: 'junit', version: '4.12' - compile project(':core-service-lib') + testCompile("org.springframework.boot:spring-boot-starter-test") + compile project(':core-service-lib') } diff --git a/core-service-lib-integration/src/main/java/org/x2b/study/core/IntegrationTestService.java b/core-service-lib-integration/src/main/java/org/x2b/study/core/IntegrationTestService.java index 40576f3..0db5d2f 100644 --- a/core-service-lib-integration/src/main/java/org/x2b/study/core/IntegrationTestService.java +++ b/core-service-lib-integration/src/main/java/org/x2b/study/core/IntegrationTestService.java @@ -1,6 +1,5 @@ package org.x2b.study.core; -import graphql.schema.StaticDataFetcher; import graphql.schema.idl.RuntimeWiring; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; diff --git a/core-service-lib-integration/src/main/java/org/x2b/study/core/graphql/fetchers/mutation/createuser/CreateUserFetcher.java b/core-service-lib-integration/src/main/java/org/x2b/study/core/graphql/fetchers/mutation/createuser/CreateUserFetcher.java index c0b5ddc..4c68e22 100644 --- a/core-service-lib-integration/src/main/java/org/x2b/study/core/graphql/fetchers/mutation/createuser/CreateUserFetcher.java +++ b/core-service-lib-integration/src/main/java/org/x2b/study/core/graphql/fetchers/mutation/createuser/CreateUserFetcher.java @@ -2,11 +2,13 @@ import graphql.schema.DataFetcher; import graphql.schema.DataFetchingEnvironment; -import org.springframework.beans.factory.annotation.Autowired; import org.x2b.study.core.security.data.mongodb.AuthenticatedUser; import org.x2b.study.core.security.data.mongodb.AuthorizationRepository; -import java.util.*; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.UUID; public class CreateUserFetcher implements DataFetcher { diff --git a/core-service-lib-integration/src/test/java/org/x2b/studi/core/TestIntegrationService.java b/core-service-lib-integration/src/test/java/org/x2b/studi/core/TestIntegrationService.java new file mode 100644 index 0000000..8d2e132 --- /dev/null +++ b/core-service-lib-integration/src/test/java/org/x2b/studi/core/TestIntegrationService.java @@ -0,0 +1,57 @@ +package org.x2b.studi.core; + +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.web.WebAppConfiguration; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.ResultMatcher; +import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder; +import org.springframework.test.web.servlet.request.MockMvcRequestBuilders; +import org.springframework.test.web.servlet.result.MockMvcResultMatchers; +import org.springframework.test.web.servlet.setup.DefaultMockMvcBuilder; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; +import org.springframework.web.context.WebApplicationContext; +import org.x2b.study.core.IntegrationTestService; + +@RunWith(SpringJUnit4ClassRunner.class) +@WebAppConfiguration +@ContextConfiguration(classes = IntegrationTestService.class) +public class TestIntegrationService { + + @Autowired + private WebApplicationContext wac; + private MockMvc mockMvc; + + + + + @Before + public void setup() { + DefaultMockMvcBuilder builder = MockMvcBuilders.webAppContextSetup(this.wac); + this.mockMvc = builder.build(); + } + + + @Test + public void contextLoads() throws Exception { + + } + + public ResultMatcher okMatcher() { + return MockMvcResultMatchers.status().isOk(); + } + + @Test + public void testGraphQlResponds() throws Exception { + ResultMatcher ok = okMatcher(); + MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/graphiql"); + mockMvc.perform(builder) + .andExpect(ok); + } + + +} diff --git a/core-service-lib-integration/src/test/resources/schema.gql b/core-service-lib-integration/src/test/resources/schema.gql new file mode 100644 index 0000000..47a2e1f --- /dev/null +++ b/core-service-lib-integration/src/test/resources/schema.gql @@ -0,0 +1,16 @@ +type MutationRoot { + createUser(input: PermissionsInput!): ID! +} + +input PermissionsInput { + permissions: [String!]! +} + +type QueryRoot { + getUserPermissions(id: ID!): [String]! +} + +schema { + mutation: MutationRoot + query: QueryRoot +} \ No newline at end of file From e7742eb3f743bfd9b4f6acb297fe79290cc0da34 Mon Sep 17 00:00:00 2001 From: Ethan Hunter Date: Tue, 10 Oct 2017 09:28:07 -0600 Subject: [PATCH 19/47] security work --- core-service-lib/build.gradle | 2 + .../study/core/GraphQLServiceConfigure.java | 6 +++ .../graphql/fetchers/SecureRootFetcher.java | 42 +++++++++++++++++++ .../shiro/JWTAuthenticationToken.java | 30 +++++++++++++ 4 files changed, 80 insertions(+) create mode 100644 core-service-lib/src/main/java/org/x2b/study/core/graphql/fetchers/SecureRootFetcher.java create mode 100644 core-service-lib/src/main/java/org/x2b/study/core/security/shiro/JWTAuthenticationToken.java diff --git a/core-service-lib/build.gradle b/core-service-lib/build.gradle index 0a64eb1..66a2148 100644 --- a/core-service-lib/build.gradle +++ b/core-service-lib/build.gradle @@ -19,4 +19,6 @@ dependencies { compile 'com.graphql-java:graphiql-spring-boot-starter:3.9.2' compile 'org.springframework.data:spring-data-mongodb:2.0.0.RELEASE' + + compile 'org.apache.shiro:shiro-all:1.2.3' } diff --git a/core-service-lib/src/main/java/org/x2b/study/core/GraphQLServiceConfigure.java b/core-service-lib/src/main/java/org/x2b/study/core/GraphQLServiceConfigure.java index f7bca87..f09e614 100644 --- a/core-service-lib/src/main/java/org/x2b/study/core/GraphQLServiceConfigure.java +++ b/core-service-lib/src/main/java/org/x2b/study/core/GraphQLServiceConfigure.java @@ -6,6 +6,7 @@ import graphql.schema.idl.SchemaGenerator; import graphql.schema.idl.SchemaParser; import graphql.schema.idl.TypeDefinitionRegistry; +import org.apache.shiro.mgt.DefaultSecurityManager; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; @@ -29,6 +30,11 @@ public GraphQLSchema schema() { return schemaGenerator.makeExecutableSchema(tdr, runtimeWiring); } + @Bean + public DefaultSecurityManager securityManager() { + return new DefaultSecurityManager(); + } + private File getSchemaFile() { return new File(this.getClass().getClassLoader().getResource(schemaFileLocation).getFile()); } diff --git a/core-service-lib/src/main/java/org/x2b/study/core/graphql/fetchers/SecureRootFetcher.java b/core-service-lib/src/main/java/org/x2b/study/core/graphql/fetchers/SecureRootFetcher.java new file mode 100644 index 0000000..8642a4e --- /dev/null +++ b/core-service-lib/src/main/java/org/x2b/study/core/graphql/fetchers/SecureRootFetcher.java @@ -0,0 +1,42 @@ +package org.x2b.study.core.graphql.fetchers; + +import graphql.schema.DataFetcher; +import graphql.schema.DataFetchingEnvironment; +import org.apache.catalina.security.SecurityUtil; +import org.apache.shiro.SecurityUtils; +import org.apache.shiro.authc.AuthenticationException; +import org.apache.shiro.authc.AuthenticationToken; +import org.apache.shiro.subject.Subject; +import org.x2b.study.core.security.shiro.JWTAuthenticationToken; + +import java.util.Map; + +/** + * Provides a secure edge for GraphQL queries. User this for all root query types to ensure that the user is properly + * logged in + * @param + */ +public abstract class SecureRootFetcher implements DataFetcher { + @Override + public T get(DataFetchingEnvironment environment) { + Subject currentUser = SecurityUtils.getSubject(); + if (!currentUser.isAuthenticated()) { + AuthenticationToken token = new JWTAuthenticationToken(getAuthTokenFromContext(environment)); + try { + currentUser.login(token); + } catch (AuthenticationException e) { + handleAuthenticationFailure(e, environment); + } + } + return secureGet(environment); + } + + + private String getAuthTokenFromContext(DataFetchingEnvironment environment) { + return null; //TODO: make this + } + + public abstract void handleAuthenticationFailure(AuthenticationException e, DataFetchingEnvironment environment); + + public abstract T secureGet(DataFetchingEnvironment environment); +} diff --git a/core-service-lib/src/main/java/org/x2b/study/core/security/shiro/JWTAuthenticationToken.java b/core-service-lib/src/main/java/org/x2b/study/core/security/shiro/JWTAuthenticationToken.java new file mode 100644 index 0000000..b0a53ec --- /dev/null +++ b/core-service-lib/src/main/java/org/x2b/study/core/security/shiro/JWTAuthenticationToken.java @@ -0,0 +1,30 @@ +package org.x2b.study.core.security.shiro; + +import org.apache.shiro.authc.AuthenticationToken; + +public class JWTAuthenticationToken implements AuthenticationToken{ + + private final String data; //TODO: eventually we need to extract the user info from this token + + public JWTAuthenticationToken(String data) { + this.data = data; + } + + @Override + public Object getPrincipal() { + return null; + } + + @Override + public Object getCredentials() { + return data; + } + + @Override + public boolean equals(Object other) { + if (other != null && other.getClass().equals(JWTAuthenticationToken.class)) { + return this.data.equals(((JWTAuthenticationToken) other).data); + } + return false; + } +} From ca54964ee56cca76ec1b524d41e987fc257e6523 Mon Sep 17 00:00:00 2001 From: Ethan Hunter Date: Mon, 16 Oct 2017 16:37:52 -0600 Subject: [PATCH 20/47] add GraphQLUtils for accessing request headers --- .../createuser/CreateUserFetcher.java | 2 ++ .../graphql/fetchers/SecureRootFetcher.java | 3 ++- .../study/core/graphql/util/GraphQLUtils.java | 25 +++++++++++++++++++ 3 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 core-service-lib/src/main/java/org/x2b/study/core/graphql/util/GraphQLUtils.java diff --git a/core-service-lib-integration/src/main/java/org/x2b/study/core/graphql/fetchers/mutation/createuser/CreateUserFetcher.java b/core-service-lib-integration/src/main/java/org/x2b/study/core/graphql/fetchers/mutation/createuser/CreateUserFetcher.java index 4c68e22..6e7033a 100644 --- a/core-service-lib-integration/src/main/java/org/x2b/study/core/graphql/fetchers/mutation/createuser/CreateUserFetcher.java +++ b/core-service-lib-integration/src/main/java/org/x2b/study/core/graphql/fetchers/mutation/createuser/CreateUserFetcher.java @@ -2,9 +2,11 @@ import graphql.schema.DataFetcher; import graphql.schema.DataFetchingEnvironment; +import graphql.servlet.GraphQLContext; import org.x2b.study.core.security.data.mongodb.AuthenticatedUser; import org.x2b.study.core.security.data.mongodb.AuthorizationRepository; +import javax.servlet.http.HttpServletRequest; import java.util.HashMap; import java.util.HashSet; import java.util.List; diff --git a/core-service-lib/src/main/java/org/x2b/study/core/graphql/fetchers/SecureRootFetcher.java b/core-service-lib/src/main/java/org/x2b/study/core/graphql/fetchers/SecureRootFetcher.java index 8642a4e..b85cf13 100644 --- a/core-service-lib/src/main/java/org/x2b/study/core/graphql/fetchers/SecureRootFetcher.java +++ b/core-service-lib/src/main/java/org/x2b/study/core/graphql/fetchers/SecureRootFetcher.java @@ -7,6 +7,7 @@ import org.apache.shiro.authc.AuthenticationException; import org.apache.shiro.authc.AuthenticationToken; import org.apache.shiro.subject.Subject; +import org.x2b.study.core.graphql.util.GraphQLUtils; import org.x2b.study.core.security.shiro.JWTAuthenticationToken; import java.util.Map; @@ -33,7 +34,7 @@ public T get(DataFetchingEnvironment environment) { private String getAuthTokenFromContext(DataFetchingEnvironment environment) { - return null; //TODO: make this + return GraphQLUtils.getHeader("authorization", environment); //TODO: make this } public abstract void handleAuthenticationFailure(AuthenticationException e, DataFetchingEnvironment environment); diff --git a/core-service-lib/src/main/java/org/x2b/study/core/graphql/util/GraphQLUtils.java b/core-service-lib/src/main/java/org/x2b/study/core/graphql/util/GraphQLUtils.java new file mode 100644 index 0000000..7aa992f --- /dev/null +++ b/core-service-lib/src/main/java/org/x2b/study/core/graphql/util/GraphQLUtils.java @@ -0,0 +1,25 @@ +package org.x2b.study.core.graphql.util; + +import graphql.schema.DataFetchingEnvironment; +import graphql.servlet.GraphQLContext; + +import javax.servlet.http.HttpServletRequest; +import java.util.Map; + +public final class GraphQLUtils { + + private GraphQLUtils() {} + + /** + * @param name The header name + * @param environment The DataFetchingEnvironment + * @return The value of the header or null if the http request does not exist + */ + public static String getHeader(String name, DataFetchingEnvironment environment) { + GraphQLContext context = environment.getContext(); + if (context.getRequest().isPresent()) { + return context.getRequest().get().getHeader(name); + } + return null; + } +} From 37d185ba116ce7c5c62689f4cab5c93d0afa5a16 Mon Sep 17 00:00:00 2001 From: Ethan Hunter Date: Mon, 16 Oct 2017 16:38:49 -0600 Subject: [PATCH 21/47] remove unused permission classes --- .../study/core/security/data/Permission.java | 27 ----- .../security/data/PermissionRepository.java | 102 ------------------ 2 files changed, 129 deletions(-) delete mode 100644 core-service-lib/src/main/java/org/x2b/study/core/security/data/Permission.java delete mode 100644 core-service-lib/src/main/java/org/x2b/study/core/security/data/PermissionRepository.java diff --git a/core-service-lib/src/main/java/org/x2b/study/core/security/data/Permission.java b/core-service-lib/src/main/java/org/x2b/study/core/security/data/Permission.java deleted file mode 100644 index c8ff82c..0000000 --- a/core-service-lib/src/main/java/org/x2b/study/core/security/data/Permission.java +++ /dev/null @@ -1,27 +0,0 @@ -package org.x2b.study.core.security.data; - -public class Permission { - - private final String permission; - - public Permission(String permission) { - this.permission = permission; - } - - public String getPermission() { - return permission; - } - - @Override - public boolean equals(Object other) { //this could thrown a null ptr if permission is null - if (other != null && other instanceof Permission) { - return this.permission.equals(((Permission) other).permission); - } - return false; - } - - @Override - public int hashCode() { - return permission.hashCode(); - } -} diff --git a/core-service-lib/src/main/java/org/x2b/study/core/security/data/PermissionRepository.java b/core-service-lib/src/main/java/org/x2b/study/core/security/data/PermissionRepository.java deleted file mode 100644 index 054a410..0000000 --- a/core-service-lib/src/main/java/org/x2b/study/core/security/data/PermissionRepository.java +++ /dev/null @@ -1,102 +0,0 @@ -package org.x2b.study.core.security.data; - -import org.springframework.stereotype.Repository; -import org.x2b.study.core.security.User; - -import java.util.Map; -import java.util.Set; - -@Repository -public interface PermissionRepository { - - /** - * Creates a user in the repository with no permissions - * @param user the user that will be added - * @return true iff successful - * - * @implNote The only stable field of user is the UUID - */ - boolean createUser(User user); - - /** - * Remove a user from the repository - * @param user the user that will be removed - * @return success - */ - boolean removeUser(User user); - - - /** - * Update a user in the repository with a new permission - * @param user The user to be updated - * @param permission The permission to add - * @return success - */ - boolean addPermissionToUser(User user, Permission permission); - - /** - * Update a user in the repository with a new permission - * @param user The user to be updated - * @param permissions The permissions to add - * @return success - */ - boolean addPermissionsToUser(User user, Set permissions); - - /** - * Update a user in the repository without a permission - * @param user The user to be updated - * @param permission The permission to remove - * @return success - */ - boolean removePermissionFromUser(User user, Permission permission); - - /** - * Update a user in the repository without a permission - * @param user The user to be updated - * @param permissions The permissions to remove - * @return success - */ - boolean removePermissionsFromUser(User user, Set permissions); - - /** - * Check if a user is present in the repository - * @param user the user to check - * @return true if the user exists, false otherwise - */ - boolean doesUserExist(User user); - - /** - * Check if a user has a permission - * @param user The user to read - * @param permission The permission to check - * @return true iff the user has the permission - */ - boolean doesUserHavePermission(User user, Permission permission); - - /** - * Check if a user has a set of permissions - * @param user The user to check - * @param permissions The set of permissions - * @return A map where each permission in the initial set is a key. Each value is true - * if the user has the permission and false otherwise. - * Returns null if the user does not exist in the repository - */ - Map doesUserHavePermissions(User user, Set permissions); - - /** - * Check if a user has every permission in a set - * @param user The user to read - * @param permissions the set of permissions - * @return true iff the user has every permission in the set - */ - boolean doesUserHaveAllPermissions(User user, Set permissions); - - - /** - * Get a set containing all of the users permissions - * @param user The user to read - * @return The set of permissions or null if the User does not exist in the - * Repository - */ - Set getAllPermissionsOfUser(User user); -} From e677913d2f666e202f44bf0a2718d2775c46d010 Mon Sep 17 00:00:00 2001 From: Ethan Hunter Date: Mon, 16 Oct 2017 17:03:54 -0600 Subject: [PATCH 22/47] work on Shiro Realm for MongoDB access --- .../data/mongodb/AuthenticatedUser.java | 2 + .../shiro/GenericAuthenticatingRealm.java | 58 +++++++++++++++++++ core-service-lib/src/main/resources/shiro.ini | 1 + 3 files changed, 61 insertions(+) create mode 100644 core-service-lib/src/main/java/org/x2b/study/core/security/shiro/GenericAuthenticatingRealm.java create mode 100644 core-service-lib/src/main/resources/shiro.ini diff --git a/core-service-lib/src/main/java/org/x2b/study/core/security/data/mongodb/AuthenticatedUser.java b/core-service-lib/src/main/java/org/x2b/study/core/security/data/mongodb/AuthenticatedUser.java index 785bda6..598398f 100644 --- a/core-service-lib/src/main/java/org/x2b/study/core/security/data/mongodb/AuthenticatedUser.java +++ b/core-service-lib/src/main/java/org/x2b/study/core/security/data/mongodb/AuthenticatedUser.java @@ -1,5 +1,7 @@ package org.x2b.study.core.security.data.mongodb; +import org.apache.shiro.authc.AuthenticationInfo; +import org.apache.shiro.subject.PrincipalCollection; import org.springframework.data.annotation.Id; import org.x2b.study.core.security.User; diff --git a/core-service-lib/src/main/java/org/x2b/study/core/security/shiro/GenericAuthenticatingRealm.java b/core-service-lib/src/main/java/org/x2b/study/core/security/shiro/GenericAuthenticatingRealm.java new file mode 100644 index 0000000..d2ea537 --- /dev/null +++ b/core-service-lib/src/main/java/org/x2b/study/core/security/shiro/GenericAuthenticatingRealm.java @@ -0,0 +1,58 @@ +package org.x2b.study.core.security.shiro; + +import org.apache.shiro.authc.AuthenticationException; +import org.apache.shiro.authc.AuthenticationInfo; +import org.apache.shiro.authc.AuthenticationToken; +import org.apache.shiro.authc.SimpleAccount; +import org.apache.shiro.realm.Realm; +import org.apache.shiro.subject.PrincipalCollection; +import org.apache.shiro.subject.SimplePrincipalCollection; +import org.springframework.beans.factory.annotation.Autowired; +import org.x2b.study.core.security.data.mongodb.AuthenticatedUser; +import org.x2b.study.core.security.data.mongodb.AuthorizationRepository; + +import java.util.UUID; + + +public class GenericAuthenticatingRealm implements Realm { + + @Autowired + private AuthorizationRepository repository; + + @Override + public String getName() { + return "AuthenticatingRealm"; + } + + @Override + public boolean supports(AuthenticationToken authenticationToken) { + return authenticationToken instanceof JWTAuthenticationToken; + } + + @Override + public AuthenticationInfo getAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException { + JWTAuthenticationToken jwtToken = (JWTAuthenticationToken) authenticationToken; + + UUID uuid = decodeUserId(jwtToken); + AuthenticatedUser user = repository.findOne(uuid); //TODO: handle missing case + + SimpleAccount account = new SimpleAccount(); + account.setCredentials(jwtToken.getCredentials()); + account.setStringPermissions(user.getPermissions()); + account.setPrincipals(createPrincipalCollection(user)); + + return account; + } + + + private PrincipalCollection createPrincipalCollection(AuthenticatedUser user) { + SimplePrincipalCollection collection = new SimplePrincipalCollection(); + collection.add(user.getUUID(), getName()); + return collection; + } + + + private UUID decodeUserId(JWTAuthenticationToken token) { + return null; + } +} diff --git a/core-service-lib/src/main/resources/shiro.ini b/core-service-lib/src/main/resources/shiro.ini new file mode 100644 index 0000000..87e5993 --- /dev/null +++ b/core-service-lib/src/main/resources/shiro.ini @@ -0,0 +1 @@ +securityManager.realms = $GenericAuthenticatingRealm \ No newline at end of file From a6c6a1c73070940c3b0404c859e4ba0ce0163aaf Mon Sep 17 00:00:00 2001 From: Ethan Hunter Date: Mon, 16 Oct 2017 17:05:33 -0600 Subject: [PATCH 23/47] maybe fix autowired for the shiro realm --- .../study/core/security/shiro/GenericAuthenticatingRealm.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/core-service-lib/src/main/java/org/x2b/study/core/security/shiro/GenericAuthenticatingRealm.java b/core-service-lib/src/main/java/org/x2b/study/core/security/shiro/GenericAuthenticatingRealm.java index d2ea537..0c9c4d4 100644 --- a/core-service-lib/src/main/java/org/x2b/study/core/security/shiro/GenericAuthenticatingRealm.java +++ b/core-service-lib/src/main/java/org/x2b/study/core/security/shiro/GenericAuthenticatingRealm.java @@ -8,12 +8,13 @@ import org.apache.shiro.subject.PrincipalCollection; import org.apache.shiro.subject.SimplePrincipalCollection; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Component; import org.x2b.study.core.security.data.mongodb.AuthenticatedUser; import org.x2b.study.core.security.data.mongodb.AuthorizationRepository; import java.util.UUID; - +@Component public class GenericAuthenticatingRealm implements Realm { @Autowired From c327a2f74e6a337cbf7e0b2889d5e4c5351899de Mon Sep 17 00:00:00 2001 From: Ethan Hunter Date: Mon, 16 Oct 2017 17:13:11 -0600 Subject: [PATCH 24/47] more auth work --- .../core/security/jwt/JWTUserRepository.java | 20 +++++++++++++++++++ .../shiro/GenericAuthenticatingRealm.java | 4 ++++ 2 files changed, 24 insertions(+) create mode 100644 core-service-lib/src/main/java/org/x2b/study/core/security/jwt/JWTUserRepository.java diff --git a/core-service-lib/src/main/java/org/x2b/study/core/security/jwt/JWTUserRepository.java b/core-service-lib/src/main/java/org/x2b/study/core/security/jwt/JWTUserRepository.java new file mode 100644 index 0000000..1d11c32 --- /dev/null +++ b/core-service-lib/src/main/java/org/x2b/study/core/security/jwt/JWTUserRepository.java @@ -0,0 +1,20 @@ +package org.x2b.study.core.security.jwt; + +import org.x2b.study.core.security.User; +import org.x2b.study.core.security.shiro.JWTAuthenticationToken; + +import java.util.UUID; + +public class JWTUserRepository { + + private boolean validateToken(JWTAuthenticationToken token) { + return true; + } + + public User getUser(JWTAuthenticationToken token) { + if (validateToken(token)) { + return () -> UUID.fromString((String) token.getPrincipal()); //TODO: this is not a good place for a lambda + } + return null; + } +} diff --git a/core-service-lib/src/main/java/org/x2b/study/core/security/shiro/GenericAuthenticatingRealm.java b/core-service-lib/src/main/java/org/x2b/study/core/security/shiro/GenericAuthenticatingRealm.java index 0c9c4d4..0aeb4b4 100644 --- a/core-service-lib/src/main/java/org/x2b/study/core/security/shiro/GenericAuthenticatingRealm.java +++ b/core-service-lib/src/main/java/org/x2b/study/core/security/shiro/GenericAuthenticatingRealm.java @@ -20,6 +20,10 @@ public class GenericAuthenticatingRealm implements Realm { @Autowired private AuthorizationRepository repository; + public GenericAuthenticatingRealm() { + + } + @Override public String getName() { return "AuthenticatingRealm"; From 1dca6821f29d1be7b60cc558c7bef9c6725c2c1b Mon Sep 17 00:00:00 2001 From: Ethan Hunter Date: Tue, 17 Oct 2017 00:33:27 -0600 Subject: [PATCH 25/47] work on auth --- core-service-lib/build.gradle | 2 ++ .../org/x2b/study/core/ServiceConstants.java | 15 ++++++++ .../graphql/fetchers/SecureRootFetcher.java | 3 +- .../core/security/jwt/JWTUserRepository.java | 35 +++++++++++++++---- .../shiro/GenericAuthenticatingRealm.java | 15 +++++--- .../shiro/JWTAuthenticationToken.java | 4 +++ 6 files changed, 63 insertions(+), 11 deletions(-) create mode 100644 core-service-lib/src/main/java/org/x2b/study/core/ServiceConstants.java diff --git a/core-service-lib/build.gradle b/core-service-lib/build.gradle index 66a2148..ac97f2a 100644 --- a/core-service-lib/build.gradle +++ b/core-service-lib/build.gradle @@ -21,4 +21,6 @@ dependencies { compile 'org.springframework.data:spring-data-mongodb:2.0.0.RELEASE' compile 'org.apache.shiro:shiro-all:1.2.3' + + compile 'com.auth0:java-jwt:3.2.0' } diff --git a/core-service-lib/src/main/java/org/x2b/study/core/ServiceConstants.java b/core-service-lib/src/main/java/org/x2b/study/core/ServiceConstants.java new file mode 100644 index 0000000..66b00ce --- /dev/null +++ b/core-service-lib/src/main/java/org/x2b/study/core/ServiceConstants.java @@ -0,0 +1,15 @@ +package org.x2b.study.core; + +public final class ServiceConstants { + + private ServiceConstants() {} + + + public static final String SECURITY_TOKEN_ISSUER = "studi_auth_service"; + public static final String SECURITY_UUID_CLAIM = "uuid"; + public static final String SECURITY_AUTHENTICATION_REALM_NAME = "generic_authentication_realm"; + + public static final String HTTP_AUTH_HEADER = "authorization"; + + public static final String DO_NOT_USE_THIS_IN_PRODUCTION = "secret"; +} diff --git a/core-service-lib/src/main/java/org/x2b/study/core/graphql/fetchers/SecureRootFetcher.java b/core-service-lib/src/main/java/org/x2b/study/core/graphql/fetchers/SecureRootFetcher.java index b85cf13..225115c 100644 --- a/core-service-lib/src/main/java/org/x2b/study/core/graphql/fetchers/SecureRootFetcher.java +++ b/core-service-lib/src/main/java/org/x2b/study/core/graphql/fetchers/SecureRootFetcher.java @@ -7,6 +7,7 @@ import org.apache.shiro.authc.AuthenticationException; import org.apache.shiro.authc.AuthenticationToken; import org.apache.shiro.subject.Subject; +import org.x2b.study.core.ServiceConstants; import org.x2b.study.core.graphql.util.GraphQLUtils; import org.x2b.study.core.security.shiro.JWTAuthenticationToken; @@ -34,7 +35,7 @@ public T get(DataFetchingEnvironment environment) { private String getAuthTokenFromContext(DataFetchingEnvironment environment) { - return GraphQLUtils.getHeader("authorization", environment); //TODO: make this + return GraphQLUtils.getHeader(ServiceConstants.HTTP_AUTH_HEADER, environment); //TODO: make this } public abstract void handleAuthenticationFailure(AuthenticationException e, DataFetchingEnvironment environment); diff --git a/core-service-lib/src/main/java/org/x2b/study/core/security/jwt/JWTUserRepository.java b/core-service-lib/src/main/java/org/x2b/study/core/security/jwt/JWTUserRepository.java index 1d11c32..3f0ed66 100644 --- a/core-service-lib/src/main/java/org/x2b/study/core/security/jwt/JWTUserRepository.java +++ b/core-service-lib/src/main/java/org/x2b/study/core/security/jwt/JWTUserRepository.java @@ -1,20 +1,43 @@ package org.x2b.study.core.security.jwt; +import com.auth0.jwt.JWT; +import com.auth0.jwt.JWTVerifier; +import com.auth0.jwt.algorithms.Algorithm; +import com.auth0.jwt.interfaces.Claim; +import com.auth0.jwt.interfaces.DecodedJWT; +import com.auth0.jwt.interfaces.RSAKeyProvider; +import org.x2b.study.core.ServiceConstants; import org.x2b.study.core.security.User; import org.x2b.study.core.security.shiro.JWTAuthenticationToken; +import sun.security.rsa.RSAPublicKeyImpl; +import java.io.UnsupportedEncodingException; +import java.security.interfaces.RSAPublicKey; import java.util.UUID; public class JWTUserRepository { - private boolean validateToken(JWTAuthenticationToken token) { - return true; + private final JWTVerifier verifier; + + public JWTUserRepository() { + Algorithm algorithm = null; + try { + algorithm = Algorithm.HMAC256(ServiceConstants.DO_NOT_USE_THIS_IN_PRODUCTION); + //TODO: Use RSA here. DON'T USE THIS IN PRODUCTION + } catch (UnsupportedEncodingException e) { + e.printStackTrace(); //TODO: This should crash at startup + } + verifier = JWT.require(algorithm) + .withIssuer(ServiceConstants.SECURITY_TOKEN_ISSUER) + .build(); } public User getUser(JWTAuthenticationToken token) { - if (validateToken(token)) { - return () -> UUID.fromString((String) token.getPrincipal()); //TODO: this is not a good place for a lambda - } - return null; + //TODO: Eventually this should use getSubject to get a JSON and then deserialize into java + DecodedJWT decodedJWT = verifier.verify(token.getToken()); + Claim uuidClaim = decodedJWT.getClaim(ServiceConstants.SECURITY_UUID_CLAIM); + String uuidString = uuidClaim.asString(); + UUID uuid = UUID.fromString(uuidString); + return () -> uuid; //TODO: this is not a good place for a lambda } } diff --git a/core-service-lib/src/main/java/org/x2b/study/core/security/shiro/GenericAuthenticatingRealm.java b/core-service-lib/src/main/java/org/x2b/study/core/security/shiro/GenericAuthenticatingRealm.java index 0aeb4b4..6c35c63 100644 --- a/core-service-lib/src/main/java/org/x2b/study/core/security/shiro/GenericAuthenticatingRealm.java +++ b/core-service-lib/src/main/java/org/x2b/study/core/security/shiro/GenericAuthenticatingRealm.java @@ -9,8 +9,11 @@ import org.apache.shiro.subject.SimplePrincipalCollection; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; +import org.x2b.study.core.ServiceConstants; +import org.x2b.study.core.security.User; import org.x2b.study.core.security.data.mongodb.AuthenticatedUser; import org.x2b.study.core.security.data.mongodb.AuthorizationRepository; +import org.x2b.study.core.security.jwt.JWTUserRepository; import java.util.UUID; @@ -20,13 +23,17 @@ public class GenericAuthenticatingRealm implements Realm { @Autowired private AuthorizationRepository repository; - public GenericAuthenticatingRealm() { + private final JWTUserRepository jwtUserRepository; + public GenericAuthenticatingRealm() { + this.jwtUserRepository = new JWTUserRepository(); //TODO: this is akward for what amounts to one method + //TODO: maybe have the JWT token decode itself so that it can actually expose and principle and + //TODO: a credential } @Override public String getName() { - return "AuthenticatingRealm"; + return ServiceConstants.SECURITY_AUTHENTICATION_REALM_NAME; } @Override @@ -38,8 +45,8 @@ public boolean supports(AuthenticationToken authenticationToken) { public AuthenticationInfo getAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException { JWTAuthenticationToken jwtToken = (JWTAuthenticationToken) authenticationToken; - UUID uuid = decodeUserId(jwtToken); - AuthenticatedUser user = repository.findOne(uuid); //TODO: handle missing case + User claimedUser = jwtUserRepository.getUser(jwtToken); + AuthenticatedUser user = repository.findOne(claimedUser.getUUID()); //TODO: handle missing case SimpleAccount account = new SimpleAccount(); account.setCredentials(jwtToken.getCredentials()); diff --git a/core-service-lib/src/main/java/org/x2b/study/core/security/shiro/JWTAuthenticationToken.java b/core-service-lib/src/main/java/org/x2b/study/core/security/shiro/JWTAuthenticationToken.java index b0a53ec..22f4ba0 100644 --- a/core-service-lib/src/main/java/org/x2b/study/core/security/shiro/JWTAuthenticationToken.java +++ b/core-service-lib/src/main/java/org/x2b/study/core/security/shiro/JWTAuthenticationToken.java @@ -17,6 +17,10 @@ public Object getPrincipal() { @Override public Object getCredentials() { + return null; + } + + public String getToken() { return data; } From 2c4c444669c714b063a3906005eddde6231ff9d0 Mon Sep 17 00:00:00 2001 From: Ethan Hunter Date: Tue, 17 Oct 2017 12:05:48 -0600 Subject: [PATCH 26/47] add a lot of hacky stuff to test shiro config --- .../study/core/IntegrationTestService.java | 6 ++++ .../mutation/getsecure/SecureTestFetcher.java | 22 ++++++++++++++ .../src/main/resources/schema.gql | 1 + .../study/core/GraphQLServiceConfigure.java | 30 +++++++++++++++++-- .../graphql/fetchers/SecureRootFetcher.java | 4 +++ .../core/security/jwt/JWTUserRepository.java | 11 +++---- .../shiro/GenericAuthenticatingRealm.java | 13 ++++---- .../shiro/JWTAuthenticationToken.java | 6 ++-- 8 files changed, 77 insertions(+), 16 deletions(-) create mode 100644 core-service-lib-integration/src/main/java/org/x2b/study/core/graphql/fetchers/mutation/getsecure/SecureTestFetcher.java diff --git a/core-service-lib-integration/src/main/java/org/x2b/study/core/IntegrationTestService.java b/core-service-lib-integration/src/main/java/org/x2b/study/core/IntegrationTestService.java index 0db5d2f..b2a5872 100644 --- a/core-service-lib-integration/src/main/java/org/x2b/study/core/IntegrationTestService.java +++ b/core-service-lib-integration/src/main/java/org/x2b/study/core/IntegrationTestService.java @@ -1,10 +1,13 @@ package org.x2b.study.core; import graphql.schema.idl.RuntimeWiring; +import org.apache.shiro.mgt.DefaultSecurityManager; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.Bean; import org.x2b.study.core.graphql.fetchers.mutation.createuser.CreateUserFetcher; +import org.x2b.study.core.graphql.fetchers.mutation.getsecure.SecureTestFetcher; import org.x2b.study.core.graphql.fetchers.query.getuser.GetUserFetcher; import org.x2b.study.core.security.data.mongodb.AuthorizationRepository; @@ -26,7 +29,10 @@ protected RuntimeWiring createRuntimeWiring() { ) .type("QueryRoot", w -> w .dataFetcher("getUserPermissions", new GetUserFetcher(authRepo)) + .dataFetcher("secureGet", new SecureTestFetcher()) ) .build(); } + + } diff --git a/core-service-lib-integration/src/main/java/org/x2b/study/core/graphql/fetchers/mutation/getsecure/SecureTestFetcher.java b/core-service-lib-integration/src/main/java/org/x2b/study/core/graphql/fetchers/mutation/getsecure/SecureTestFetcher.java new file mode 100644 index 0000000..28bcf7c --- /dev/null +++ b/core-service-lib-integration/src/main/java/org/x2b/study/core/graphql/fetchers/mutation/getsecure/SecureTestFetcher.java @@ -0,0 +1,22 @@ +package org.x2b.study.core.graphql.fetchers.mutation.getsecure; + +import graphql.schema.DataFetchingEnvironment; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; +import org.apache.shiro.SecurityUtils; +import org.apache.shiro.authc.AuthenticationException; +import org.x2b.study.core.graphql.fetchers.SecureRootFetcher; + +public class SecureTestFetcher extends SecureRootFetcher { + private static final Log log = LogFactory.getLog(SecureTestFetcher.class); + + @Override + public void handleAuthenticationFailure(AuthenticationException e, DataFetchingEnvironment environment) { + log.debug("auth failure!"); + } + + @Override + public Object secureGet(DataFetchingEnvironment environment) { + return SecurityUtils.getSubject().getPrincipal(); + } +} diff --git a/core-service-lib-integration/src/main/resources/schema.gql b/core-service-lib-integration/src/main/resources/schema.gql index 47a2e1f..515d684 100644 --- a/core-service-lib-integration/src/main/resources/schema.gql +++ b/core-service-lib-integration/src/main/resources/schema.gql @@ -8,6 +8,7 @@ input PermissionsInput { type QueryRoot { getUserPermissions(id: ID!): [String]! + secureGet: String! } schema { diff --git a/core-service-lib/src/main/java/org/x2b/study/core/GraphQLServiceConfigure.java b/core-service-lib/src/main/java/org/x2b/study/core/GraphQLServiceConfigure.java index f09e614..6bf5b7e 100644 --- a/core-service-lib/src/main/java/org/x2b/study/core/GraphQLServiceConfigure.java +++ b/core-service-lib/src/main/java/org/x2b/study/core/GraphQLServiceConfigure.java @@ -7,8 +7,13 @@ import graphql.schema.idl.SchemaParser; import graphql.schema.idl.TypeDefinitionRegistry; import org.apache.shiro.mgt.DefaultSecurityManager; +import org.apache.shiro.spring.web.ShiroFilterFactoryBean; +import org.apache.shiro.web.mgt.DefaultWebSecurityManager; import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.context.annotation.Bean; +import org.springframework.web.filter.DelegatingFilterProxy; +import org.x2b.study.core.security.shiro.GenericAuthenticatingRealm; import java.io.File; @@ -31,8 +36,29 @@ public GraphQLSchema schema() { } @Bean - public DefaultSecurityManager securityManager() { - return new DefaultSecurityManager(); + public DefaultWebSecurityManager securityManager() { + DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager(); + securityManager.setRealm(new GenericAuthenticatingRealm()); + return securityManager; + } + + @Bean + public FilterRegistrationBean shrioFilterRegistration() { + FilterRegistrationBean filterRegistration = new FilterRegistrationBean(); + DelegatingFilterProxy delegatingFilterProxy = new DelegatingFilterProxy(); + delegatingFilterProxy.setTargetBeanName("shiroFilter"); + filterRegistration.setFilter(new DelegatingFilterProxy()); + filterRegistration.setName("shiroFilter"); + filterRegistration.addInitParameter("targetFilterLifecycle", "true"); + filterRegistration.addUrlPatterns("/*"); + return filterRegistration; + } + + @Bean + public ShiroFilterFactoryBean shiroFilter() { + ShiroFilterFactoryBean shiroFilterFactory = new ShiroFilterFactoryBean(); + shiroFilterFactory.setSecurityManager(securityManager()); + return shiroFilterFactory; } private File getSchemaFile() { diff --git a/core-service-lib/src/main/java/org/x2b/study/core/graphql/fetchers/SecureRootFetcher.java b/core-service-lib/src/main/java/org/x2b/study/core/graphql/fetchers/SecureRootFetcher.java index 225115c..a4b5953 100644 --- a/core-service-lib/src/main/java/org/x2b/study/core/graphql/fetchers/SecureRootFetcher.java +++ b/core-service-lib/src/main/java/org/x2b/study/core/graphql/fetchers/SecureRootFetcher.java @@ -6,6 +6,8 @@ import org.apache.shiro.SecurityUtils; import org.apache.shiro.authc.AuthenticationException; import org.apache.shiro.authc.AuthenticationToken; +import org.apache.shiro.mgt.RealmSecurityManager; +import org.apache.shiro.realm.Realm; import org.apache.shiro.subject.Subject; import org.x2b.study.core.ServiceConstants; import org.x2b.study.core.graphql.util.GraphQLUtils; @@ -21,6 +23,8 @@ public abstract class SecureRootFetcher implements DataFetcher { @Override public T get(DataFetchingEnvironment environment) { + for (Realm realm : ((RealmSecurityManager) SecurityUtils.getSecurityManager()).getRealms()) + System.out.println(realm.getName()); Subject currentUser = SecurityUtils.getSubject(); if (!currentUser.isAuthenticated()) { AuthenticationToken token = new JWTAuthenticationToken(getAuthTokenFromContext(environment)); diff --git a/core-service-lib/src/main/java/org/x2b/study/core/security/jwt/JWTUserRepository.java b/core-service-lib/src/main/java/org/x2b/study/core/security/jwt/JWTUserRepository.java index 3f0ed66..3195c28 100644 --- a/core-service-lib/src/main/java/org/x2b/study/core/security/jwt/JWTUserRepository.java +++ b/core-service-lib/src/main/java/org/x2b/study/core/security/jwt/JWTUserRepository.java @@ -34,10 +34,11 @@ public JWTUserRepository() { public User getUser(JWTAuthenticationToken token) { //TODO: Eventually this should use getSubject to get a JSON and then deserialize into java - DecodedJWT decodedJWT = verifier.verify(token.getToken()); - Claim uuidClaim = decodedJWT.getClaim(ServiceConstants.SECURITY_UUID_CLAIM); - String uuidString = uuidClaim.asString(); - UUID uuid = UUID.fromString(uuidString); - return () -> uuid; //TODO: this is not a good place for a lambda +// DecodedJWT decodedJWT = verifier.verify(token.getToken()); +// Claim uuidClaim = decodedJWT.getClaim(ServiceConstants.SECURITY_UUID_CLAIM); +// String uuidString = uuidClaim.asString(); +// UUID uuid = UUID.fromString(uuidString); + + return () -> UUID.fromString(token.getToken()); //TODO: this is not a good place for a lambda } } diff --git a/core-service-lib/src/main/java/org/x2b/study/core/security/shiro/GenericAuthenticatingRealm.java b/core-service-lib/src/main/java/org/x2b/study/core/security/shiro/GenericAuthenticatingRealm.java index 6c35c63..c3ec81f 100644 --- a/core-service-lib/src/main/java/org/x2b/study/core/security/shiro/GenericAuthenticatingRealm.java +++ b/core-service-lib/src/main/java/org/x2b/study/core/security/shiro/GenericAuthenticatingRealm.java @@ -4,6 +4,7 @@ import org.apache.shiro.authc.AuthenticationInfo; import org.apache.shiro.authc.AuthenticationToken; import org.apache.shiro.authc.SimpleAccount; +import org.apache.shiro.authc.pam.ModularRealmAuthenticator; import org.apache.shiro.realm.Realm; import org.apache.shiro.subject.PrincipalCollection; import org.apache.shiro.subject.SimplePrincipalCollection; @@ -46,18 +47,18 @@ public AuthenticationInfo getAuthenticationInfo(AuthenticationToken authenticati JWTAuthenticationToken jwtToken = (JWTAuthenticationToken) authenticationToken; User claimedUser = jwtUserRepository.getUser(jwtToken); - AuthenticatedUser user = repository.findOne(claimedUser.getUUID()); //TODO: handle missing case + //AuthenticatedUser user = repository.findOne(claimedUser.getUUID()); //TODO: handle missing case - SimpleAccount account = new SimpleAccount(); - account.setCredentials(jwtToken.getCredentials()); - account.setStringPermissions(user.getPermissions()); - account.setPrincipals(createPrincipalCollection(user)); + SimpleAccount account = new SimpleAccount(jwtToken.getPrincipal(), jwtToken.getCredentials(), getName()); + //account.setCredentials(jwtToken.getCredentials()); + //account.setStringPermissions(user.getPermissions()); + //account.setPrincipals(createPrincipalCollection(claimedUser)); return account; } - private PrincipalCollection createPrincipalCollection(AuthenticatedUser user) { + private PrincipalCollection createPrincipalCollection(User user) { SimplePrincipalCollection collection = new SimplePrincipalCollection(); collection.add(user.getUUID(), getName()); return collection; diff --git a/core-service-lib/src/main/java/org/x2b/study/core/security/shiro/JWTAuthenticationToken.java b/core-service-lib/src/main/java/org/x2b/study/core/security/shiro/JWTAuthenticationToken.java index 22f4ba0..a77ada4 100644 --- a/core-service-lib/src/main/java/org/x2b/study/core/security/shiro/JWTAuthenticationToken.java +++ b/core-service-lib/src/main/java/org/x2b/study/core/security/shiro/JWTAuthenticationToken.java @@ -4,7 +4,7 @@ public class JWTAuthenticationToken implements AuthenticationToken{ - private final String data; //TODO: eventually we need to extract the user info from this token + private final String data; public JWTAuthenticationToken(String data) { this.data = data; @@ -12,12 +12,12 @@ public JWTAuthenticationToken(String data) { @Override public Object getPrincipal() { - return null; + return data; } @Override public Object getCredentials() { - return null; + return data; } public String getToken() { From 097889a9b35da5f536982b094bdb553ba21a60ac Mon Sep 17 00:00:00 2001 From: Ethan Hunter Date: Tue, 17 Oct 2017 19:25:40 -0600 Subject: [PATCH 27/47] break everything --- .../org/x2b/study/core/IntegrationTestService.java | 7 ++++++- .../x2b/study/core/GraphQLServiceConfigure.java | 1 + .../security/shiro/GenericAuthenticatingRealm.java | 14 ++++++++------ 3 files changed, 15 insertions(+), 7 deletions(-) diff --git a/core-service-lib-integration/src/main/java/org/x2b/study/core/IntegrationTestService.java b/core-service-lib-integration/src/main/java/org/x2b/study/core/IntegrationTestService.java index b2a5872..8261820 100644 --- a/core-service-lib-integration/src/main/java/org/x2b/study/core/IntegrationTestService.java +++ b/core-service-lib-integration/src/main/java/org/x2b/study/core/IntegrationTestService.java @@ -5,18 +5,23 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.ApplicationContext; +import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; import org.x2b.study.core.graphql.fetchers.mutation.createuser.CreateUserFetcher; import org.x2b.study.core.graphql.fetchers.mutation.getsecure.SecureTestFetcher; import org.x2b.study.core.graphql.fetchers.query.getuser.GetUserFetcher; import org.x2b.study.core.security.data.mongodb.AuthorizationRepository; @SpringBootApplication +@ComponentScan(basePackages = "org.x2b.study.core.*") public class IntegrationTestService extends GraphQLServiceConfigure { public static void main(String[] args) { - SpringApplication.run(IntegrationTestService.class, args); + ConfigurableApplicationContext ctx = SpringApplication.run(IntegrationTestService.class, args); + System.out.println(ctx.getBean("GenericAuthenticatingRealm")); } @Autowired diff --git a/core-service-lib/src/main/java/org/x2b/study/core/GraphQLServiceConfigure.java b/core-service-lib/src/main/java/org/x2b/study/core/GraphQLServiceConfigure.java index 6bf5b7e..964588c 100644 --- a/core-service-lib/src/main/java/org/x2b/study/core/GraphQLServiceConfigure.java +++ b/core-service-lib/src/main/java/org/x2b/study/core/GraphQLServiceConfigure.java @@ -12,6 +12,7 @@ import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; import org.springframework.web.filter.DelegatingFilterProxy; import org.x2b.study.core.security.shiro.GenericAuthenticatingRealm; diff --git a/core-service-lib/src/main/java/org/x2b/study/core/security/shiro/GenericAuthenticatingRealm.java b/core-service-lib/src/main/java/org/x2b/study/core/security/shiro/GenericAuthenticatingRealm.java index c3ec81f..7d95831 100644 --- a/core-service-lib/src/main/java/org/x2b/study/core/security/shiro/GenericAuthenticatingRealm.java +++ b/core-service-lib/src/main/java/org/x2b/study/core/security/shiro/GenericAuthenticatingRealm.java @@ -9,20 +9,22 @@ import org.apache.shiro.subject.PrincipalCollection; import org.apache.shiro.subject.SimplePrincipalCollection; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.ComponentScan; import org.springframework.stereotype.Component; +import org.springframework.stereotype.Service; import org.x2b.study.core.ServiceConstants; import org.x2b.study.core.security.User; import org.x2b.study.core.security.data.mongodb.AuthenticatedUser; import org.x2b.study.core.security.data.mongodb.AuthorizationRepository; import org.x2b.study.core.security.jwt.JWTUserRepository; - import java.util.UUID; @Component public class GenericAuthenticatingRealm implements Realm { @Autowired - private AuthorizationRepository repository; + public AuthorizationRepository repository; private final JWTUserRepository jwtUserRepository; @@ -47,12 +49,12 @@ public AuthenticationInfo getAuthenticationInfo(AuthenticationToken authenticati JWTAuthenticationToken jwtToken = (JWTAuthenticationToken) authenticationToken; User claimedUser = jwtUserRepository.getUser(jwtToken); - //AuthenticatedUser user = repository.findOne(claimedUser.getUUID()); //TODO: handle missing case + AuthenticatedUser user = repository.findOne(claimedUser.getUUID()); //TODO: handle missing case SimpleAccount account = new SimpleAccount(jwtToken.getPrincipal(), jwtToken.getCredentials(), getName()); - //account.setCredentials(jwtToken.getCredentials()); - //account.setStringPermissions(user.getPermissions()); - //account.setPrincipals(createPrincipalCollection(claimedUser)); + account.setCredentials(jwtToken.getCredentials()); + account.setStringPermissions(user.getPermissions()); + account.setPrincipals(createPrincipalCollection(claimedUser)); return account; } From d7fc8b163caca6089f09280d90bd897676176338 Mon Sep 17 00:00:00 2001 From: Ethan Hunter Date: Wed, 18 Oct 2017 10:47:48 -0600 Subject: [PATCH 28/47] break shiro config and fix bean config --- .../study/core/IntegrationTestService.java | 2 +- .../mutation/getsecure/SecureTestFetcher.java | 5 ++++- .../study/core/GraphQLServiceConfigure.java | 8 +++++++- .../core/security/shiro/AuthorizingRealm.java | 19 +++++++++++++++++++ .../shiro/GenericAuthenticatingRealm.java | 1 + 5 files changed, 32 insertions(+), 3 deletions(-) create mode 100644 core-service-lib/src/main/java/org/x2b/study/core/security/shiro/AuthorizingRealm.java diff --git a/core-service-lib-integration/src/main/java/org/x2b/study/core/IntegrationTestService.java b/core-service-lib-integration/src/main/java/org/x2b/study/core/IntegrationTestService.java index 8261820..e8d6968 100644 --- a/core-service-lib-integration/src/main/java/org/x2b/study/core/IntegrationTestService.java +++ b/core-service-lib-integration/src/main/java/org/x2b/study/core/IntegrationTestService.java @@ -21,7 +21,7 @@ public class IntegrationTestService extends GraphQLServiceConfigure { public static void main(String[] args) { ConfigurableApplicationContext ctx = SpringApplication.run(IntegrationTestService.class, args); - System.out.println(ctx.getBean("GenericAuthenticatingRealm")); + System.out.println(ctx.getBean("authenticatingRealm")); } @Autowired diff --git a/core-service-lib-integration/src/main/java/org/x2b/study/core/graphql/fetchers/mutation/getsecure/SecureTestFetcher.java b/core-service-lib-integration/src/main/java/org/x2b/study/core/graphql/fetchers/mutation/getsecure/SecureTestFetcher.java index 28bcf7c..c10aa37 100644 --- a/core-service-lib-integration/src/main/java/org/x2b/study/core/graphql/fetchers/mutation/getsecure/SecureTestFetcher.java +++ b/core-service-lib-integration/src/main/java/org/x2b/study/core/graphql/fetchers/mutation/getsecure/SecureTestFetcher.java @@ -5,6 +5,7 @@ import org.apache.commons.logging.LogFactory; import org.apache.shiro.SecurityUtils; import org.apache.shiro.authc.AuthenticationException; +import org.apache.shiro.subject.Subject; import org.x2b.study.core.graphql.fetchers.SecureRootFetcher; public class SecureTestFetcher extends SecureRootFetcher { @@ -17,6 +18,8 @@ public void handleAuthenticationFailure(AuthenticationException e, DataFetchingE @Override public Object secureGet(DataFetchingEnvironment environment) { - return SecurityUtils.getSubject().getPrincipal(); + Subject s = SecurityUtils.getSubject(); + + return s.isPermitted("foo:bar:read"); } } diff --git a/core-service-lib/src/main/java/org/x2b/study/core/GraphQLServiceConfigure.java b/core-service-lib/src/main/java/org/x2b/study/core/GraphQLServiceConfigure.java index 964588c..df72ee1 100644 --- a/core-service-lib/src/main/java/org/x2b/study/core/GraphQLServiceConfigure.java +++ b/core-service-lib/src/main/java/org/x2b/study/core/GraphQLServiceConfigure.java @@ -11,6 +11,7 @@ import org.apache.shiro.web.mgt.DefaultWebSecurityManager; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.web.servlet.FilterRegistrationBean; +import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.web.filter.DelegatingFilterProxy; @@ -36,10 +37,15 @@ public GraphQLSchema schema() { return schemaGenerator.makeExecutableSchema(tdr, runtimeWiring); } + @Bean + public GenericAuthenticatingRealm authenticatingRealm() { + return new GenericAuthenticatingRealm(); + } + @Bean public DefaultWebSecurityManager securityManager() { DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager(); - securityManager.setRealm(new GenericAuthenticatingRealm()); + securityManager.setRealm(authenticatingRealm()); return securityManager; } diff --git a/core-service-lib/src/main/java/org/x2b/study/core/security/shiro/AuthorizingRealm.java b/core-service-lib/src/main/java/org/x2b/study/core/security/shiro/AuthorizingRealm.java new file mode 100644 index 0000000..c7c16bf --- /dev/null +++ b/core-service-lib/src/main/java/org/x2b/study/core/security/shiro/AuthorizingRealm.java @@ -0,0 +1,19 @@ +package org.x2b.study.core.security.shiro; + +import org.apache.shiro.authc.AuthenticationException; +import org.apache.shiro.authc.AuthenticationInfo; +import org.apache.shiro.authc.AuthenticationToken; +import org.apache.shiro.authz.AuthorizationInfo; +import org.apache.shiro.subject.PrincipalCollection; + +public class AuthorizingRealm extends org.apache.shiro.realm.AuthorizingRealm { + @Override + protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) { + return null; + } + + @Override + protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException { + return null; + } +} diff --git a/core-service-lib/src/main/java/org/x2b/study/core/security/shiro/GenericAuthenticatingRealm.java b/core-service-lib/src/main/java/org/x2b/study/core/security/shiro/GenericAuthenticatingRealm.java index 7d95831..5749525 100644 --- a/core-service-lib/src/main/java/org/x2b/study/core/security/shiro/GenericAuthenticatingRealm.java +++ b/core-service-lib/src/main/java/org/x2b/study/core/security/shiro/GenericAuthenticatingRealm.java @@ -5,6 +5,7 @@ import org.apache.shiro.authc.AuthenticationToken; import org.apache.shiro.authc.SimpleAccount; import org.apache.shiro.authc.pam.ModularRealmAuthenticator; +import org.apache.shiro.authz.Authorizer; import org.apache.shiro.realm.Realm; import org.apache.shiro.subject.PrincipalCollection; import org.apache.shiro.subject.SimplePrincipalCollection; From 9663243cdc16e156490127d318c0da76fbb607a5 Mon Sep 17 00:00:00 2001 From: Ethan Hunter Date: Thu, 19 Oct 2017 16:55:49 -0600 Subject: [PATCH 29/47] fix shiro session management. Currently caching is broken --- .../study/core/IntegrationTestService.java | 3 -- .../createuser/CreateUserFetcher.java | 2 -- .../study/core/GraphQLServiceConfigure.java | 23 ++++++++++--- .../graphql/fetchers/SecureRootFetcher.java | 3 -- .../study/core/graphql/util/GraphQLUtils.java | 3 -- .../data/mongodb/AuthenticatedUser.java | 2 -- .../core/security/jwt/JWTUserRepository.java | 5 --- .../core/security/shiro/AuthorizingRealm.java | 19 ----------- .../shiro/GenericAuthenticatingRealm.java | 34 ++++++++++--------- core-service-lib/src/main/resources/shiro.ini | 2 ++ 10 files changed, 39 insertions(+), 57 deletions(-) delete mode 100644 core-service-lib/src/main/java/org/x2b/study/core/security/shiro/AuthorizingRealm.java diff --git a/core-service-lib-integration/src/main/java/org/x2b/study/core/IntegrationTestService.java b/core-service-lib-integration/src/main/java/org/x2b/study/core/IntegrationTestService.java index e8d6968..fe5fb9d 100644 --- a/core-service-lib-integration/src/main/java/org/x2b/study/core/IntegrationTestService.java +++ b/core-service-lib-integration/src/main/java/org/x2b/study/core/IntegrationTestService.java @@ -1,13 +1,10 @@ package org.x2b.study.core; import graphql.schema.idl.RuntimeWiring; -import org.apache.shiro.mgt.DefaultSecurityManager; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.context.ApplicationContext; import org.springframework.context.ConfigurableApplicationContext; -import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.x2b.study.core.graphql.fetchers.mutation.createuser.CreateUserFetcher; import org.x2b.study.core.graphql.fetchers.mutation.getsecure.SecureTestFetcher; diff --git a/core-service-lib-integration/src/main/java/org/x2b/study/core/graphql/fetchers/mutation/createuser/CreateUserFetcher.java b/core-service-lib-integration/src/main/java/org/x2b/study/core/graphql/fetchers/mutation/createuser/CreateUserFetcher.java index 6e7033a..4c68e22 100644 --- a/core-service-lib-integration/src/main/java/org/x2b/study/core/graphql/fetchers/mutation/createuser/CreateUserFetcher.java +++ b/core-service-lib-integration/src/main/java/org/x2b/study/core/graphql/fetchers/mutation/createuser/CreateUserFetcher.java @@ -2,11 +2,9 @@ import graphql.schema.DataFetcher; import graphql.schema.DataFetchingEnvironment; -import graphql.servlet.GraphQLContext; import org.x2b.study.core.security.data.mongodb.AuthenticatedUser; import org.x2b.study.core.security.data.mongodb.AuthorizationRepository; -import javax.servlet.http.HttpServletRequest; import java.util.HashMap; import java.util.HashSet; import java.util.List; diff --git a/core-service-lib/src/main/java/org/x2b/study/core/GraphQLServiceConfigure.java b/core-service-lib/src/main/java/org/x2b/study/core/GraphQLServiceConfigure.java index df72ee1..5f57cb2 100644 --- a/core-service-lib/src/main/java/org/x2b/study/core/GraphQLServiceConfigure.java +++ b/core-service-lib/src/main/java/org/x2b/study/core/GraphQLServiceConfigure.java @@ -6,14 +6,14 @@ import graphql.schema.idl.SchemaGenerator; import graphql.schema.idl.SchemaParser; import graphql.schema.idl.TypeDefinitionRegistry; -import org.apache.shiro.mgt.DefaultSecurityManager; +import org.apache.shiro.mgt.DefaultSessionStorageEvaluator; +import org.apache.shiro.mgt.DefaultSubjectDAO; import org.apache.shiro.spring.web.ShiroFilterFactoryBean; +import org.apache.shiro.web.filter.session.NoSessionCreationFilter; import org.apache.shiro.web.mgt.DefaultWebSecurityManager; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.web.servlet.FilterRegistrationBean; -import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.ComponentScan; import org.springframework.web.filter.DelegatingFilterProxy; import org.x2b.study.core.security.shiro.GenericAuthenticatingRealm; @@ -46,6 +46,11 @@ public GenericAuthenticatingRealm authenticatingRealm() { public DefaultWebSecurityManager securityManager() { DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager(); securityManager.setRealm(authenticatingRealm()); + DefaultSessionStorageEvaluator sessionStorageEvaluator = new DefaultSessionStorageEvaluator(); + sessionStorageEvaluator.setSessionStorageEnabled(false); + DefaultSubjectDAO subjectDAO = new DefaultSubjectDAO(); + subjectDAO.setSessionStorageEvaluator(sessionStorageEvaluator); + securityManager.setSubjectDAO(subjectDAO); return securityManager; } @@ -54,13 +59,23 @@ public FilterRegistrationBean shrioFilterRegistration() { FilterRegistrationBean filterRegistration = new FilterRegistrationBean(); DelegatingFilterProxy delegatingFilterProxy = new DelegatingFilterProxy(); delegatingFilterProxy.setTargetBeanName("shiroFilter"); - filterRegistration.setFilter(new DelegatingFilterProxy()); + filterRegistration.setFilter(delegatingFilterProxy); filterRegistration.setName("shiroFilter"); filterRegistration.addInitParameter("targetFilterLifecycle", "true"); filterRegistration.addUrlPatterns("/*"); return filterRegistration; } + @Bean + public FilterRegistrationBean shrioSessionFilterRegistration() { + FilterRegistrationBean filterRegistration = new FilterRegistrationBean(); + filterRegistration.setFilter(new NoSessionCreationFilter()); + filterRegistration.setName("shiroSessionFilter"); + filterRegistration.addInitParameter("targetFilterLifecycle", "true"); + filterRegistration.addUrlPatterns("/*"); + return filterRegistration; + } + @Bean public ShiroFilterFactoryBean shiroFilter() { ShiroFilterFactoryBean shiroFilterFactory = new ShiroFilterFactoryBean(); diff --git a/core-service-lib/src/main/java/org/x2b/study/core/graphql/fetchers/SecureRootFetcher.java b/core-service-lib/src/main/java/org/x2b/study/core/graphql/fetchers/SecureRootFetcher.java index a4b5953..52cb6ed 100644 --- a/core-service-lib/src/main/java/org/x2b/study/core/graphql/fetchers/SecureRootFetcher.java +++ b/core-service-lib/src/main/java/org/x2b/study/core/graphql/fetchers/SecureRootFetcher.java @@ -2,7 +2,6 @@ import graphql.schema.DataFetcher; import graphql.schema.DataFetchingEnvironment; -import org.apache.catalina.security.SecurityUtil; import org.apache.shiro.SecurityUtils; import org.apache.shiro.authc.AuthenticationException; import org.apache.shiro.authc.AuthenticationToken; @@ -13,8 +12,6 @@ import org.x2b.study.core.graphql.util.GraphQLUtils; import org.x2b.study.core.security.shiro.JWTAuthenticationToken; -import java.util.Map; - /** * Provides a secure edge for GraphQL queries. User this for all root query types to ensure that the user is properly * logged in diff --git a/core-service-lib/src/main/java/org/x2b/study/core/graphql/util/GraphQLUtils.java b/core-service-lib/src/main/java/org/x2b/study/core/graphql/util/GraphQLUtils.java index 7aa992f..6db6fc0 100644 --- a/core-service-lib/src/main/java/org/x2b/study/core/graphql/util/GraphQLUtils.java +++ b/core-service-lib/src/main/java/org/x2b/study/core/graphql/util/GraphQLUtils.java @@ -3,9 +3,6 @@ import graphql.schema.DataFetchingEnvironment; import graphql.servlet.GraphQLContext; -import javax.servlet.http.HttpServletRequest; -import java.util.Map; - public final class GraphQLUtils { private GraphQLUtils() {} diff --git a/core-service-lib/src/main/java/org/x2b/study/core/security/data/mongodb/AuthenticatedUser.java b/core-service-lib/src/main/java/org/x2b/study/core/security/data/mongodb/AuthenticatedUser.java index 598398f..785bda6 100644 --- a/core-service-lib/src/main/java/org/x2b/study/core/security/data/mongodb/AuthenticatedUser.java +++ b/core-service-lib/src/main/java/org/x2b/study/core/security/data/mongodb/AuthenticatedUser.java @@ -1,7 +1,5 @@ package org.x2b.study.core.security.data.mongodb; -import org.apache.shiro.authc.AuthenticationInfo; -import org.apache.shiro.subject.PrincipalCollection; import org.springframework.data.annotation.Id; import org.x2b.study.core.security.User; diff --git a/core-service-lib/src/main/java/org/x2b/study/core/security/jwt/JWTUserRepository.java b/core-service-lib/src/main/java/org/x2b/study/core/security/jwt/JWTUserRepository.java index 3195c28..8d4f9ed 100644 --- a/core-service-lib/src/main/java/org/x2b/study/core/security/jwt/JWTUserRepository.java +++ b/core-service-lib/src/main/java/org/x2b/study/core/security/jwt/JWTUserRepository.java @@ -3,16 +3,11 @@ import com.auth0.jwt.JWT; import com.auth0.jwt.JWTVerifier; import com.auth0.jwt.algorithms.Algorithm; -import com.auth0.jwt.interfaces.Claim; -import com.auth0.jwt.interfaces.DecodedJWT; -import com.auth0.jwt.interfaces.RSAKeyProvider; import org.x2b.study.core.ServiceConstants; import org.x2b.study.core.security.User; import org.x2b.study.core.security.shiro.JWTAuthenticationToken; -import sun.security.rsa.RSAPublicKeyImpl; import java.io.UnsupportedEncodingException; -import java.security.interfaces.RSAPublicKey; import java.util.UUID; public class JWTUserRepository { diff --git a/core-service-lib/src/main/java/org/x2b/study/core/security/shiro/AuthorizingRealm.java b/core-service-lib/src/main/java/org/x2b/study/core/security/shiro/AuthorizingRealm.java deleted file mode 100644 index c7c16bf..0000000 --- a/core-service-lib/src/main/java/org/x2b/study/core/security/shiro/AuthorizingRealm.java +++ /dev/null @@ -1,19 +0,0 @@ -package org.x2b.study.core.security.shiro; - -import org.apache.shiro.authc.AuthenticationException; -import org.apache.shiro.authc.AuthenticationInfo; -import org.apache.shiro.authc.AuthenticationToken; -import org.apache.shiro.authz.AuthorizationInfo; -import org.apache.shiro.subject.PrincipalCollection; - -public class AuthorizingRealm extends org.apache.shiro.realm.AuthorizingRealm { - @Override - protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) { - return null; - } - - @Override - protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException { - return null; - } -} diff --git a/core-service-lib/src/main/java/org/x2b/study/core/security/shiro/GenericAuthenticatingRealm.java b/core-service-lib/src/main/java/org/x2b/study/core/security/shiro/GenericAuthenticatingRealm.java index 5749525..2d27773 100644 --- a/core-service-lib/src/main/java/org/x2b/study/core/security/shiro/GenericAuthenticatingRealm.java +++ b/core-service-lib/src/main/java/org/x2b/study/core/security/shiro/GenericAuthenticatingRealm.java @@ -3,26 +3,24 @@ import org.apache.shiro.authc.AuthenticationException; import org.apache.shiro.authc.AuthenticationInfo; import org.apache.shiro.authc.AuthenticationToken; -import org.apache.shiro.authc.SimpleAccount; -import org.apache.shiro.authc.pam.ModularRealmAuthenticator; -import org.apache.shiro.authz.Authorizer; -import org.apache.shiro.realm.Realm; +import org.apache.shiro.authc.SimpleAuthenticationInfo; +import org.apache.shiro.authz.AuthorizationInfo; +import org.apache.shiro.authz.SimpleAuthorizationInfo; +import org.apache.shiro.realm.AuthorizingRealm; import org.apache.shiro.subject.PrincipalCollection; import org.apache.shiro.subject.SimplePrincipalCollection; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.ComponentScan; import org.springframework.stereotype.Component; -import org.springframework.stereotype.Service; import org.x2b.study.core.ServiceConstants; import org.x2b.study.core.security.User; import org.x2b.study.core.security.data.mongodb.AuthenticatedUser; import org.x2b.study.core.security.data.mongodb.AuthorizationRepository; import org.x2b.study.core.security.jwt.JWTUserRepository; + import java.util.UUID; @Component -public class GenericAuthenticatingRealm implements Realm { +public class GenericAuthenticatingRealm extends AuthorizingRealm { @Autowired public AuthorizationRepository repository; @@ -46,18 +44,22 @@ public boolean supports(AuthenticationToken authenticationToken) { } @Override - public AuthenticationInfo getAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException { + public AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException { JWTAuthenticationToken jwtToken = (JWTAuthenticationToken) authenticationToken; User claimedUser = jwtUserRepository.getUser(jwtToken); - AuthenticatedUser user = repository.findOne(claimedUser.getUUID()); //TODO: handle missing case - - SimpleAccount account = new SimpleAccount(jwtToken.getPrincipal(), jwtToken.getCredentials(), getName()); - account.setCredentials(jwtToken.getCredentials()); - account.setStringPermissions(user.getPermissions()); - account.setPrincipals(createPrincipalCollection(claimedUser)); + SimpleAuthenticationInfo authenticationInfo = + new SimpleAuthenticationInfo(claimedUser, authenticationToken.getCredentials(), getName()); + return authenticationInfo; + } - return account; + @Override + protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) { + User user = (User) principalCollection.getPrimaryPrincipal(); + AuthenticatedUser authorizedUser = repository.findOne(user.getUUID()); + SimpleAuthorizationInfo info = new SimpleAuthorizationInfo(); + info.setStringPermissions(authorizedUser.getPermissions()); + return info; } diff --git a/core-service-lib/src/main/resources/shiro.ini b/core-service-lib/src/main/resources/shiro.ini index 87e5993..6273c6d 100644 --- a/core-service-lib/src/main/resources/shiro.ini +++ b/core-service-lib/src/main/resources/shiro.ini @@ -1 +1,3 @@ +[main] +securityManager.subjectDAO.sessionStorageEvaluator.sessionStorageEnabled = false securityManager.realms = $GenericAuthenticatingRealm \ No newline at end of file From 7d8c1084d23f9ec5b7ac9cb0bc5b7a9147a27252 Mon Sep 17 00:00:00 2001 From: Ethan Hunter Date: Thu, 19 Oct 2017 17:13:41 -0600 Subject: [PATCH 30/47] code cleanup --- .../study/core/IntegrationTestService.java | 1 + .../study/core/GraphQLServiceConfigure.java | 23 +++++++------------ 2 files changed, 9 insertions(+), 15 deletions(-) diff --git a/core-service-lib-integration/src/main/java/org/x2b/study/core/IntegrationTestService.java b/core-service-lib-integration/src/main/java/org/x2b/study/core/IntegrationTestService.java index fe5fb9d..abb4688 100644 --- a/core-service-lib-integration/src/main/java/org/x2b/study/core/IntegrationTestService.java +++ b/core-service-lib-integration/src/main/java/org/x2b/study/core/IntegrationTestService.java @@ -24,6 +24,7 @@ public static void main(String[] args) { @Autowired public AuthorizationRepository authRepo; + @Override protected RuntimeWiring createRuntimeWiring() { return RuntimeWiring.newRuntimeWiring() .type("MutationRoot", w -> w diff --git a/core-service-lib/src/main/java/org/x2b/study/core/GraphQLServiceConfigure.java b/core-service-lib/src/main/java/org/x2b/study/core/GraphQLServiceConfigure.java index 5f57cb2..069c6ed 100644 --- a/core-service-lib/src/main/java/org/x2b/study/core/GraphQLServiceConfigure.java +++ b/core-service-lib/src/main/java/org/x2b/study/core/GraphQLServiceConfigure.java @@ -8,6 +8,7 @@ import graphql.schema.idl.TypeDefinitionRegistry; import org.apache.shiro.mgt.DefaultSessionStorageEvaluator; import org.apache.shiro.mgt.DefaultSubjectDAO; +import org.apache.shiro.mgt.SubjectDAO; import org.apache.shiro.spring.web.ShiroFilterFactoryBean; import org.apache.shiro.web.filter.session.NoSessionCreationFilter; import org.apache.shiro.web.mgt.DefaultWebSecurityManager; @@ -46,12 +47,16 @@ public GenericAuthenticatingRealm authenticatingRealm() { public DefaultWebSecurityManager securityManager() { DefaultWebSecurityManager securityManager = new DefaultWebSecurityManager(); securityManager.setRealm(authenticatingRealm()); + securityManager.setSubjectDAO(createStatelessSubjectDao()); + return securityManager; + } + + private SubjectDAO createStatelessSubjectDao() { DefaultSessionStorageEvaluator sessionStorageEvaluator = new DefaultSessionStorageEvaluator(); sessionStorageEvaluator.setSessionStorageEnabled(false); DefaultSubjectDAO subjectDAO = new DefaultSubjectDAO(); subjectDAO.setSessionStorageEvaluator(sessionStorageEvaluator); - securityManager.setSubjectDAO(subjectDAO); - return securityManager; + return subjectDAO; } @Bean @@ -66,16 +71,6 @@ public FilterRegistrationBean shrioFilterRegistration() { return filterRegistration; } - @Bean - public FilterRegistrationBean shrioSessionFilterRegistration() { - FilterRegistrationBean filterRegistration = new FilterRegistrationBean(); - filterRegistration.setFilter(new NoSessionCreationFilter()); - filterRegistration.setName("shiroSessionFilter"); - filterRegistration.addInitParameter("targetFilterLifecycle", "true"); - filterRegistration.addUrlPatterns("/*"); - return filterRegistration; - } - @Bean public ShiroFilterFactoryBean shiroFilter() { ShiroFilterFactoryBean shiroFilterFactory = new ShiroFilterFactoryBean(); @@ -88,7 +83,5 @@ private File getSchemaFile() { } - protected RuntimeWiring createRuntimeWiring() { - return null; - } + protected abstract RuntimeWiring createRuntimeWiring(); } From f9eefdfa6cf3a6d592c0e433c8b9bb5c26293af9 Mon Sep 17 00:00:00 2001 From: Ethan Hunter Date: Thu, 19 Oct 2017 17:42:47 -0600 Subject: [PATCH 31/47] fix auth header name to comply to standard --- .../src/main/java/org/x2b/study/core/ServiceConstants.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core-service-lib/src/main/java/org/x2b/study/core/ServiceConstants.java b/core-service-lib/src/main/java/org/x2b/study/core/ServiceConstants.java index 66b00ce..cdbf112 100644 --- a/core-service-lib/src/main/java/org/x2b/study/core/ServiceConstants.java +++ b/core-service-lib/src/main/java/org/x2b/study/core/ServiceConstants.java @@ -9,7 +9,7 @@ private ServiceConstants() {} public static final String SECURITY_UUID_CLAIM = "uuid"; public static final String SECURITY_AUTHENTICATION_REALM_NAME = "generic_authentication_realm"; - public static final String HTTP_AUTH_HEADER = "authorization"; + public static final String HTTP_AUTH_HEADER = "Authorization"; public static final String DO_NOT_USE_THIS_IN_PRODUCTION = "secret"; } From 2bea31f71dd6fe55b0abe1c89fe64dfa7f5d3e1e Mon Sep 17 00:00:00 2001 From: Ethan Hunter Date: Thu, 19 Oct 2017 18:03:06 -0600 Subject: [PATCH 32/47] enable the most basic of caching for the auth db --- .../x2b/study/core/IntegrationTestService.java | 2 ++ .../x2b/study/core/GraphQLServiceConfigure.java | 1 + .../data/mongodb/AuthorizationRepository.java | 15 +++++++++++++++ .../shiro/GenericAuthenticatingRealm.java | 1 + 4 files changed, 19 insertions(+) diff --git a/core-service-lib-integration/src/main/java/org/x2b/study/core/IntegrationTestService.java b/core-service-lib-integration/src/main/java/org/x2b/study/core/IntegrationTestService.java index abb4688..e153b07 100644 --- a/core-service-lib-integration/src/main/java/org/x2b/study/core/IntegrationTestService.java +++ b/core-service-lib-integration/src/main/java/org/x2b/study/core/IntegrationTestService.java @@ -4,6 +4,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cache.annotation.EnableCaching; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.ComponentScan; import org.x2b.study.core.graphql.fetchers.mutation.createuser.CreateUserFetcher; @@ -13,6 +14,7 @@ @SpringBootApplication @ComponentScan(basePackages = "org.x2b.study.core.*") +@EnableCaching public class IntegrationTestService extends GraphQLServiceConfigure { diff --git a/core-service-lib/src/main/java/org/x2b/study/core/GraphQLServiceConfigure.java b/core-service-lib/src/main/java/org/x2b/study/core/GraphQLServiceConfigure.java index 069c6ed..6796aa0 100644 --- a/core-service-lib/src/main/java/org/x2b/study/core/GraphQLServiceConfigure.java +++ b/core-service-lib/src/main/java/org/x2b/study/core/GraphQLServiceConfigure.java @@ -14,6 +14,7 @@ import org.apache.shiro.web.mgt.DefaultWebSecurityManager; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.web.servlet.FilterRegistrationBean; +import org.springframework.cache.annotation.EnableCaching; import org.springframework.context.annotation.Bean; import org.springframework.web.filter.DelegatingFilterProxy; import org.x2b.study.core.security.shiro.GenericAuthenticatingRealm; diff --git a/core-service-lib/src/main/java/org/x2b/study/core/security/data/mongodb/AuthorizationRepository.java b/core-service-lib/src/main/java/org/x2b/study/core/security/data/mongodb/AuthorizationRepository.java index 71e4f5c..60ad9d2 100644 --- a/core-service-lib/src/main/java/org/x2b/study/core/security/data/mongodb/AuthorizationRepository.java +++ b/core-service-lib/src/main/java/org/x2b/study/core/security/data/mongodb/AuthorizationRepository.java @@ -1,8 +1,23 @@ package org.x2b.study.core.security.data.mongodb; +import org.springframework.cache.annotation.CacheEvict; +import org.springframework.cache.annotation.Cacheable; +import org.springframework.data.domain.Example; import org.springframework.data.mongodb.repository.MongoRepository; +import java.util.List; import java.util.UUID; public interface AuthorizationRepository extends MongoRepository { + + String AUTHORIZED_USER_CACHE = "authorizedUser"; + + @Override + @Cacheable(AUTHORIZED_USER_CACHE) + AuthenticatedUser findOne(UUID uuid); + + + @Override + @CacheEvict(AUTHORIZED_USER_CACHE) + AuthenticatedUser save(AuthenticatedUser entity); } diff --git a/core-service-lib/src/main/java/org/x2b/study/core/security/shiro/GenericAuthenticatingRealm.java b/core-service-lib/src/main/java/org/x2b/study/core/security/shiro/GenericAuthenticatingRealm.java index 2d27773..187f9f2 100644 --- a/core-service-lib/src/main/java/org/x2b/study/core/security/shiro/GenericAuthenticatingRealm.java +++ b/core-service-lib/src/main/java/org/x2b/study/core/security/shiro/GenericAuthenticatingRealm.java @@ -22,6 +22,7 @@ @Component public class GenericAuthenticatingRealm extends AuthorizingRealm { + @Autowired public AuthorizationRepository repository; From ae416b582267e9c2dd3454d3d2d3bd008d30da18 Mon Sep 17 00:00:00 2001 From: Ethan Hunter Date: Thu, 19 Oct 2017 18:05:39 -0600 Subject: [PATCH 33/47] fix AuthenticatedUser not being suitable for caching --- .../security/data/mongodb/AuthenticatedUser.java | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/core-service-lib/src/main/java/org/x2b/study/core/security/data/mongodb/AuthenticatedUser.java b/core-service-lib/src/main/java/org/x2b/study/core/security/data/mongodb/AuthenticatedUser.java index 785bda6..952d264 100644 --- a/core-service-lib/src/main/java/org/x2b/study/core/security/data/mongodb/AuthenticatedUser.java +++ b/core-service-lib/src/main/java/org/x2b/study/core/security/data/mongodb/AuthenticatedUser.java @@ -36,4 +36,17 @@ public UUID getUUID() { public String toString() { return String.format("User: %s", id); } + + @Override + public boolean equals(Object other) { + if (other != null && other instanceof AuthenticatedUser) { + return ((AuthenticatedUser) other).id.equals(id); + } + return false; + } + + @Override + public int hashCode() { + return id.hashCode(); + } } From 6a3cd7423563e839db0bb30520f87089478a06d6 Mon Sep 17 00:00:00 2001 From: Ethan Hunter Date: Fri, 20 Oct 2017 11:21:14 -0600 Subject: [PATCH 34/47] more code cleanup + disable Shiro-level caching --- .../security/shiro/GenericAuthenticatingRealm.java | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/core-service-lib/src/main/java/org/x2b/study/core/security/shiro/GenericAuthenticatingRealm.java b/core-service-lib/src/main/java/org/x2b/study/core/security/shiro/GenericAuthenticatingRealm.java index 187f9f2..979d460 100644 --- a/core-service-lib/src/main/java/org/x2b/study/core/security/shiro/GenericAuthenticatingRealm.java +++ b/core-service-lib/src/main/java/org/x2b/study/core/security/shiro/GenericAuthenticatingRealm.java @@ -29,6 +29,7 @@ public class GenericAuthenticatingRealm extends AuthorizingRealm { private final JWTUserRepository jwtUserRepository; public GenericAuthenticatingRealm() { + this.setCachingEnabled(false); //TODO: maybe just don't extend a caching realm this.jwtUserRepository = new JWTUserRepository(); //TODO: this is akward for what amounts to one method //TODO: maybe have the JWT token decode itself so that it can actually expose and principle and //TODO: a credential @@ -62,16 +63,4 @@ protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principal info.setStringPermissions(authorizedUser.getPermissions()); return info; } - - - private PrincipalCollection createPrincipalCollection(User user) { - SimplePrincipalCollection collection = new SimplePrincipalCollection(); - collection.add(user.getUUID(), getName()); - return collection; - } - - - private UUID decodeUserId(JWTAuthenticationToken token) { - return null; - } } From a45e2c58ac28433e97f93410989e2771493d8e99 Mon Sep 17 00:00:00 2001 From: Ethan Hunter Date: Fri, 20 Oct 2017 11:21:48 -0600 Subject: [PATCH 35/47] remove unused shiro.ini --- core-service-lib/src/main/resources/shiro.ini | 3 --- 1 file changed, 3 deletions(-) delete mode 100644 core-service-lib/src/main/resources/shiro.ini diff --git a/core-service-lib/src/main/resources/shiro.ini b/core-service-lib/src/main/resources/shiro.ini deleted file mode 100644 index 6273c6d..0000000 --- a/core-service-lib/src/main/resources/shiro.ini +++ /dev/null @@ -1,3 +0,0 @@ -[main] -securityManager.subjectDAO.sessionStorageEvaluator.sessionStorageEnabled = false -securityManager.realms = $GenericAuthenticatingRealm \ No newline at end of file From c6d3b890bafbebeb14c07eb13e04e298deef42e0 Mon Sep 17 00:00:00 2001 From: Ethan Hunter Date: Fri, 20 Oct 2017 18:01:43 -0600 Subject: [PATCH 36/47] work on adding rsa to jwt signing --- .../study/core/GraphQLServiceConfigure.java | 19 ++++ ...ository.java => JWTUserTokenVerifier.java} | 24 ++++-- .../core/security/jwt/KeyProviderBean.java | 86 +++++++++++++++++++ .../shiro/GenericAuthenticatingRealm.java | 13 +-- studi_test_keys | 27 ++++++ studi_test_keys.pub | 1 + 6 files changed, 152 insertions(+), 18 deletions(-) rename core-service-lib/src/main/java/org/x2b/study/core/security/jwt/{JWTUserRepository.java => JWTUserTokenVerifier.java} (59%) create mode 100644 core-service-lib/src/main/java/org/x2b/study/core/security/jwt/KeyProviderBean.java create mode 100644 studi_test_keys create mode 100644 studi_test_keys.pub diff --git a/core-service-lib/src/main/java/org/x2b/study/core/GraphQLServiceConfigure.java b/core-service-lib/src/main/java/org/x2b/study/core/GraphQLServiceConfigure.java index 6796aa0..04ef26c 100644 --- a/core-service-lib/src/main/java/org/x2b/study/core/GraphQLServiceConfigure.java +++ b/core-service-lib/src/main/java/org/x2b/study/core/GraphQLServiceConfigure.java @@ -12,20 +12,29 @@ import org.apache.shiro.spring.web.ShiroFilterFactoryBean; import org.apache.shiro.web.filter.session.NoSessionCreationFilter; import org.apache.shiro.web.mgt.DefaultWebSecurityManager; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.cache.annotation.EnableCaching; +import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.web.filter.DelegatingFilterProxy; +import org.x2b.study.core.security.jwt.JWTUserTokenVerifier; +import org.x2b.study.core.security.jwt.KeyProviderBean; import org.x2b.study.core.security.shiro.GenericAuthenticatingRealm; import java.io.File; +import java.io.IOException; +import java.security.GeneralSecurityException; public abstract class GraphQLServiceConfigure { @Value("#{graphql.schema.schemaFileLocation}") public static String schemaFileLocation = "schema.gql"; + @Autowired + protected ApplicationContext applicationContext; + @Bean public GraphQLSchema schema() { @@ -79,6 +88,16 @@ public ShiroFilterFactoryBean shiroFilter() { return shiroFilterFactory; } + @Bean + public KeyProviderBean jwtAuthKeyProvider() { + return new KeyProviderBean(); + } + + @Bean + public JWTUserTokenVerifier jwtUserTokenVerifier() { + return new JWTUserTokenVerifier((KeyProviderBean) applicationContext.getBean("jwtAuthKeyProvider")); + } + private File getSchemaFile() { return new File(this.getClass().getClassLoader().getResource(schemaFileLocation).getFile()); } diff --git a/core-service-lib/src/main/java/org/x2b/study/core/security/jwt/JWTUserRepository.java b/core-service-lib/src/main/java/org/x2b/study/core/security/jwt/JWTUserTokenVerifier.java similarity index 59% rename from core-service-lib/src/main/java/org/x2b/study/core/security/jwt/JWTUserRepository.java rename to core-service-lib/src/main/java/org/x2b/study/core/security/jwt/JWTUserTokenVerifier.java index 8d4f9ed..cc9b01c 100644 --- a/core-service-lib/src/main/java/org/x2b/study/core/security/jwt/JWTUserRepository.java +++ b/core-service-lib/src/main/java/org/x2b/study/core/security/jwt/JWTUserTokenVerifier.java @@ -3,25 +3,31 @@ import com.auth0.jwt.JWT; import com.auth0.jwt.JWTVerifier; import com.auth0.jwt.algorithms.Algorithm; +import com.auth0.jwt.interfaces.RSAKeyProvider; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; import org.x2b.study.core.ServiceConstants; import org.x2b.study.core.security.User; import org.x2b.study.core.security.shiro.JWTAuthenticationToken; +import sun.security.rsa.RSAPublicKeyImpl; import java.io.UnsupportedEncodingException; +import java.security.interfaces.RSAPrivateKey; +import java.security.interfaces.RSAPublicKey; import java.util.UUID; -public class JWTUserRepository { +@Component +public class JWTUserTokenVerifier { private final JWTVerifier verifier; - public JWTUserRepository() { - Algorithm algorithm = null; - try { - algorithm = Algorithm.HMAC256(ServiceConstants.DO_NOT_USE_THIS_IN_PRODUCTION); - //TODO: Use RSA here. DON'T USE THIS IN PRODUCTION - } catch (UnsupportedEncodingException e) { - e.printStackTrace(); //TODO: This should crash at startup - } + @Autowired + public JWTUserTokenVerifier(KeyProviderBean rsaKeyProvider) { + RSAPublicKey publicKey = rsaKeyProvider.getPublicKey(); + RSAPrivateKey privateKey = rsaKeyProvider.getPrivateKey(); + Algorithm algorithm = Algorithm.RSA256(publicKey, privateKey); verifier = JWT.require(algorithm) .withIssuer(ServiceConstants.SECURITY_TOKEN_ISSUER) .build(); diff --git a/core-service-lib/src/main/java/org/x2b/study/core/security/jwt/KeyProviderBean.java b/core-service-lib/src/main/java/org/x2b/study/core/security/jwt/KeyProviderBean.java new file mode 100644 index 0000000..33a13f6 --- /dev/null +++ b/core-service-lib/src/main/java/org/x2b/study/core/security/jwt/KeyProviderBean.java @@ -0,0 +1,86 @@ +package org.x2b.study.core.security.jwt; + +import org.apache.shiro.codec.Base64; +import org.bouncycastle.crypto.params.AsymmetricKeyParameter; +import org.bouncycastle.crypto.params.ECKeyParameters; +import org.bouncycastle.crypto.util.PrivateKeyFactory; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; + +import javax.annotation.PostConstruct; +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.security.*; +import java.security.cert.CertificateException; +import java.security.cert.CertificateFactory; +import java.security.cert.X509Certificate; +import java.security.interfaces.RSAPrivateKey; +import java.security.interfaces.RSAPublicKey; +import java.security.spec.InvalidKeySpecException; +import java.security.spec.PKCS8EncodedKeySpec; +import java.security.spec.X509EncodedKeySpec; + +@Component +public class KeyProviderBean { + + @Value("${auth.rsa.public-key}") + private String encodedPublicKey; + + @Value("${auth.rsa.private-key}") + private String encodedPrivateKey; + + private RSAPublicKey publicKey; + private RSAPrivateKey privateKey; + + + @PostConstruct //TODO: I hate this + private void setUpKeys() throws IOException, GeneralSecurityException { + KeyFactory rsaKeyFactory = KeyFactory.getInstance("RSA"); + publicKey = getPublicKey(encodedPublicKey, rsaKeyFactory); + privateKey = getPrivateKey(encodedPrivateKey, rsaKeyFactory); + System.out.println(publicKey.getAlgorithm()); + System.out.println(privateKey.getAlgorithm()); + } + + private RSAPublicKey getPublicKey(final String publicKeyStr, final KeyFactory keyFactory) + throws IOException, InvalidKeySpecException, CertificateException, NoSuchProviderException { + if (publicKeyStr == null) { + return null; + } + CertificateFactory cf = CertificateFactory.getInstance("X509", "BC"); + try (ByteArrayInputStream bis = new ByteArrayInputStream(publicKeyStr.getBytes())) { + X509Certificate cert = (X509Certificate) cf.generateCertificate(bis); + return (RSAPublicKey) cert.getPublicKey(); + } + } + + private RSAPrivateKey getPrivateKey(final String privateKeyStr, final KeyFactory keyFactory) + throws IOException, InvalidKeySpecException, CertificateException, NoSuchProviderException { + if (privateKeyStr == null) { + return null; + } + return null; + } + + private byte[] getKeyBytes(final String key) throws UnsupportedEncodingException { + return Base64.decode(key.getBytes("utf-8")); + } + + /** + * Get this provider's public key + * @return null iff a public key is not defined + */ + public RSAPublicKey getPublicKey() { + return publicKey; + } + + /** + * Get this providers private key + * @return null iff a private key is not defined + */ + public RSAPrivateKey getPrivateKey() { + return privateKey; + } + +} diff --git a/core-service-lib/src/main/java/org/x2b/study/core/security/shiro/GenericAuthenticatingRealm.java b/core-service-lib/src/main/java/org/x2b/study/core/security/shiro/GenericAuthenticatingRealm.java index 979d460..adc564b 100644 --- a/core-service-lib/src/main/java/org/x2b/study/core/security/shiro/GenericAuthenticatingRealm.java +++ b/core-service-lib/src/main/java/org/x2b/study/core/security/shiro/GenericAuthenticatingRealm.java @@ -8,16 +8,13 @@ import org.apache.shiro.authz.SimpleAuthorizationInfo; import org.apache.shiro.realm.AuthorizingRealm; import org.apache.shiro.subject.PrincipalCollection; -import org.apache.shiro.subject.SimplePrincipalCollection; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.x2b.study.core.ServiceConstants; import org.x2b.study.core.security.User; import org.x2b.study.core.security.data.mongodb.AuthenticatedUser; import org.x2b.study.core.security.data.mongodb.AuthorizationRepository; -import org.x2b.study.core.security.jwt.JWTUserRepository; - -import java.util.UUID; +import org.x2b.study.core.security.jwt.JWTUserTokenVerifier; @Component public class GenericAuthenticatingRealm extends AuthorizingRealm { @@ -26,13 +23,11 @@ public class GenericAuthenticatingRealm extends AuthorizingRealm { @Autowired public AuthorizationRepository repository; - private final JWTUserRepository jwtUserRepository; + @Autowired + private JWTUserTokenVerifier jwtUserTokenVerifier; public GenericAuthenticatingRealm() { this.setCachingEnabled(false); //TODO: maybe just don't extend a caching realm - this.jwtUserRepository = new JWTUserRepository(); //TODO: this is akward for what amounts to one method - //TODO: maybe have the JWT token decode itself so that it can actually expose and principle and - //TODO: a credential } @Override @@ -49,7 +44,7 @@ public boolean supports(AuthenticationToken authenticationToken) { public AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException { JWTAuthenticationToken jwtToken = (JWTAuthenticationToken) authenticationToken; - User claimedUser = jwtUserRepository.getUser(jwtToken); + User claimedUser = jwtUserTokenVerifier.getUser(jwtToken); SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(claimedUser, authenticationToken.getCredentials(), getName()); return authenticationInfo; diff --git a/studi_test_keys b/studi_test_keys new file mode 100644 index 0000000..2ccd97b --- /dev/null +++ b/studi_test_keys @@ -0,0 +1,27 @@ +-----BEGIN RSA PRIVATE KEY----- +MIIEowIBAAKCAQEAlCRUCS1JVX4tF8K/dX23CKCMs2D1JlDf+kDBJCzrgVAnyKJt +rlUAyj35hg/JZC6MG4IcEspfyFDn6rvG1HqUqUVaSKPFO99rUd38k6skomZcCQKd +6m9+RYNpsr0/PKYwqaJBJhCSqAVnXLizbYqN0ucdZSzG4QNx37pyzkOiJmtD2Ujt +G0ECkwI60DRrNIEdeGvbJb89OS8w2KbtAUcFAv9ZetTFN3pleRjdgnOVlWCznhXl +qiq237WOSit53w5WrVVyG/k5Rt87BAcEp9BwVJ6N3tszuyvE3IeVnIW2jgWS4PRr +VKwELQ+uAMZwo+6o6pobwS9YbNHj/Q6tyYCckwIDAQABAoIBAAsRK7YDzBa29mdw +BR2h5qqeU+SSub3DDPiFHei18mIWuuIiQE9lyklRl/KL+1+gmpbzgWVl+QHeCXVf +0drNzY1XwQbsNS/j4QCctOHTR0lanEWQDHwXGDfZOWxA9xhw0fHaTUOquUoUHUSw +B7eHQlEfMvL8BV+KAlfUdf8yzkpQXAgwGnX7j9T7NtwmB8+lDk7phrr/GPfSIWiR +3CiP18YofoxGfJ2MZHA740ySFqjy6sYx2dwtKbWvvhFeloLE1c+F3w7bfcXpsHOC +HtmFVtgJQiq8yoVawow7qTI8siMtBQx6OR59V0gkvTffdTNb0bc0rS3UpAxe6anI +OSo3lwECgYEAxCOfwLx1OPl/nculBsM6waLRR4RLaL88oXfuj2gvgBIAoQP04mxa +3js7CFqYFpBmiXsEbOD0CPfpfVk2JUK10lQMCiDzpzJE1DhQSLAxo0WDNVdRvj1b +W+Yw/9MB/v/t8i+3o0f7Kvyop/yuSaq5qqOaYCENX8danWlaB9JIwJUCgYEAwVqs +YZg05S3ZADA52htEyuUW72ua8WVJ665aa0Z6OH80BFh+YBKZkXUQrjrRSbyHWwUg +hxyBqmj54giO2mVfmQky5GMLml+Bzq5MXNKXQKWwkF9vP9Y3vEG521XBZZMn7f/q +gHue+l3Jdkw3Qm8dTZim0utU59yG2wmRW2FtVocCgYBfE0WlD7Vi4M5Tlxtvo8Mf +ieUYXbCQHeMuAgzJxx7/XbqgUmKqPwvtHZkH+6ItjeGmUtNaU9D3Z/TupgfxF907 +DzKm1kJFkcB7frCsC3yEQHXRrXoaVXOroHGwEICvMzAMbcT+iS3fMIowo/N4EiHd +7WAvqNXqz5/FHgReNji94QKBgQCkFxFoGcX6IPiiIktWgAHXtqfKNCXgazdanjL6 +C2u5+qnobjmBywcs3I51blEjJK4yXp2GTHj2Z/R5EQ465gFFXPslu7CjKB5/iogf +1lCqenLnczZ0lJXkXFsgexyP2VzvrFoVA9SdYW6RyIi1Bpt1uPEK2tDJnOuNhAcp +5/rkjQKBgHiex/sd1LdefdRNZXfNBH+DAxdHbXYRjBFJyUuY3sH/jZzsYGC3G7nS +honrTkt3GvTG05DAdgJw766KV9FZ3pq4LvaQu6SchS5vh58ckvJFvAtnkhyfyl3k +pgCtNo64OmRqCU6x2yDVAoKAYll2lC02KwzwhfYYGpz8Ypjjb+IY +-----END RSA PRIVATE KEY----- diff --git a/studi_test_keys.pub b/studi_test_keys.pub new file mode 100644 index 0000000..6c89c2a --- /dev/null +++ b/studi_test_keys.pub @@ -0,0 +1 @@ +ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCUJFQJLUlVfi0Xwr91fbcIoIyzYPUmUN/6QMEkLOuBUCfIom2uVQDKPfmGD8lkLowbghwSyl/IUOfqu8bUepSpRVpIo8U732tR3fyTqySiZlwJAp3qb35Fg2myvT88pjCpokEmEJKoBWdcuLNtio3S5x1lLMbhA3HfunLOQ6Ima0PZSO0bQQKTAjrQNGs0gR14a9slvz05LzDYpu0BRwUC/1l61MU3emV5GN2Cc5WVYLOeFeWqKrbftY5KK3nfDlatVXIb+TlG3zsEBwSn0HBUno3e2zO7K8Tch5WchbaOBZLg9GtUrAQtD64AxnCj7qjqmhvBL1hs0eP9Dq3JgJyT ethan@res2-212-16-dhcp.int.colorado.edu From 1aa8c4f31d4c859c00f80ce642729f15bb7a3e24 Mon Sep 17 00:00:00 2001 From: Ethan Hunter Date: Fri, 20 Oct 2017 18:02:58 -0600 Subject: [PATCH 37/47] remove test keys from repo - STILL DON'T USE THEM --- studi_test_keys | 27 --------------------------- studi_test_keys.pub | 1 - 2 files changed, 28 deletions(-) delete mode 100644 studi_test_keys delete mode 100644 studi_test_keys.pub diff --git a/studi_test_keys b/studi_test_keys deleted file mode 100644 index 2ccd97b..0000000 --- a/studi_test_keys +++ /dev/null @@ -1,27 +0,0 @@ ------BEGIN RSA PRIVATE KEY----- -MIIEowIBAAKCAQEAlCRUCS1JVX4tF8K/dX23CKCMs2D1JlDf+kDBJCzrgVAnyKJt -rlUAyj35hg/JZC6MG4IcEspfyFDn6rvG1HqUqUVaSKPFO99rUd38k6skomZcCQKd -6m9+RYNpsr0/PKYwqaJBJhCSqAVnXLizbYqN0ucdZSzG4QNx37pyzkOiJmtD2Ujt -G0ECkwI60DRrNIEdeGvbJb89OS8w2KbtAUcFAv9ZetTFN3pleRjdgnOVlWCznhXl -qiq237WOSit53w5WrVVyG/k5Rt87BAcEp9BwVJ6N3tszuyvE3IeVnIW2jgWS4PRr -VKwELQ+uAMZwo+6o6pobwS9YbNHj/Q6tyYCckwIDAQABAoIBAAsRK7YDzBa29mdw -BR2h5qqeU+SSub3DDPiFHei18mIWuuIiQE9lyklRl/KL+1+gmpbzgWVl+QHeCXVf -0drNzY1XwQbsNS/j4QCctOHTR0lanEWQDHwXGDfZOWxA9xhw0fHaTUOquUoUHUSw -B7eHQlEfMvL8BV+KAlfUdf8yzkpQXAgwGnX7j9T7NtwmB8+lDk7phrr/GPfSIWiR -3CiP18YofoxGfJ2MZHA740ySFqjy6sYx2dwtKbWvvhFeloLE1c+F3w7bfcXpsHOC -HtmFVtgJQiq8yoVawow7qTI8siMtBQx6OR59V0gkvTffdTNb0bc0rS3UpAxe6anI -OSo3lwECgYEAxCOfwLx1OPl/nculBsM6waLRR4RLaL88oXfuj2gvgBIAoQP04mxa -3js7CFqYFpBmiXsEbOD0CPfpfVk2JUK10lQMCiDzpzJE1DhQSLAxo0WDNVdRvj1b -W+Yw/9MB/v/t8i+3o0f7Kvyop/yuSaq5qqOaYCENX8danWlaB9JIwJUCgYEAwVqs -YZg05S3ZADA52htEyuUW72ua8WVJ665aa0Z6OH80BFh+YBKZkXUQrjrRSbyHWwUg -hxyBqmj54giO2mVfmQky5GMLml+Bzq5MXNKXQKWwkF9vP9Y3vEG521XBZZMn7f/q -gHue+l3Jdkw3Qm8dTZim0utU59yG2wmRW2FtVocCgYBfE0WlD7Vi4M5Tlxtvo8Mf -ieUYXbCQHeMuAgzJxx7/XbqgUmKqPwvtHZkH+6ItjeGmUtNaU9D3Z/TupgfxF907 -DzKm1kJFkcB7frCsC3yEQHXRrXoaVXOroHGwEICvMzAMbcT+iS3fMIowo/N4EiHd -7WAvqNXqz5/FHgReNji94QKBgQCkFxFoGcX6IPiiIktWgAHXtqfKNCXgazdanjL6 -C2u5+qnobjmBywcs3I51blEjJK4yXp2GTHj2Z/R5EQ465gFFXPslu7CjKB5/iogf -1lCqenLnczZ0lJXkXFsgexyP2VzvrFoVA9SdYW6RyIi1Bpt1uPEK2tDJnOuNhAcp -5/rkjQKBgHiex/sd1LdefdRNZXfNBH+DAxdHbXYRjBFJyUuY3sH/jZzsYGC3G7nS -honrTkt3GvTG05DAdgJw766KV9FZ3pq4LvaQu6SchS5vh58ckvJFvAtnkhyfyl3k -pgCtNo64OmRqCU6x2yDVAoKAYll2lC02KwzwhfYYGpz8Ypjjb+IY ------END RSA PRIVATE KEY----- diff --git a/studi_test_keys.pub b/studi_test_keys.pub deleted file mode 100644 index 6c89c2a..0000000 --- a/studi_test_keys.pub +++ /dev/null @@ -1 +0,0 @@ -ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCUJFQJLUlVfi0Xwr91fbcIoIyzYPUmUN/6QMEkLOuBUCfIom2uVQDKPfmGD8lkLowbghwSyl/IUOfqu8bUepSpRVpIo8U732tR3fyTqySiZlwJAp3qb35Fg2myvT88pjCpokEmEJKoBWdcuLNtio3S5x1lLMbhA3HfunLOQ6Ima0PZSO0bQQKTAjrQNGs0gR14a9slvz05LzDYpu0BRwUC/1l61MU3emV5GN2Cc5WVYLOeFeWqKrbftY5KK3nfDlatVXIb+TlG3zsEBwSn0HBUno3e2zO7K8Tch5WchbaOBZLg9GtUrAQtD64AxnCj7qjqmhvBL1hs0eP9Dq3JgJyT ethan@res2-212-16-dhcp.int.colorado.edu From 71a50540e5e43ec3d6d6bd608ec581a58beee5d5 Mon Sep 17 00:00:00 2001 From: Ethan Hunter Date: Sat, 21 Oct 2017 01:26:58 -0600 Subject: [PATCH 38/47] rename packages --- .../core/IntegrationTestService.java | 25 +++--- .../createuser/CreateUserFetcher.java | 6 +- .../mutation/getsecure/SecureTestFetcher.java | 4 +- .../query/getuser/GetUserFetcher.java | 6 +- .../studi/core/TestIntegrationService.java | 1 - .../core/GraphQLServiceConfigure.java | 19 ++-- .../core/ServiceConstants.java | 2 +- .../graphql/errors/UnauthorizedException.java | 2 +- .../graphql/fetchers/SecureRootFetcher.java | 8 +- .../core/graphql/util/GraphQLUtils.java | 2 +- .../{study => studi}/core/security/User.java | 2 +- .../data/mongodb/AuthenticatedUser.java | 6 +- .../data/mongodb/AuthorizationRepository.java | 4 +- .../security/jwt/JWTUserTokenVerifier.java | 24 ++---- .../security/jwt/SharedSecretProvider.java | 21 +++++ .../shiro/GenericAuthenticatingRealm.java | 12 +-- .../shiro/JWTAuthenticationToken.java | 2 +- .../core/security/jwt/KeyProviderBean.java | 86 ------------------- .../core/TestGraphQLServiceConfigure.java | 2 +- 19 files changed, 80 insertions(+), 154 deletions(-) rename core-service-lib-integration/src/main/java/org/x2b/{study => studi}/core/IntegrationTestService.java (59%) rename core-service-lib-integration/src/main/java/org/x2b/{study => studi}/core/graphql/fetchers/mutation/createuser/CreateUserFetcher.java (81%) rename core-service-lib-integration/src/main/java/org/x2b/{study => studi}/core/graphql/fetchers/mutation/getsecure/SecureTestFetcher.java (85%) rename core-service-lib-integration/src/main/java/org/x2b/{study => studi}/core/graphql/fetchers/query/getuser/GetUserFetcher.java (78%) rename core-service-lib/src/main/java/org/x2b/{study => studi}/core/GraphQLServiceConfigure.java (85%) rename core-service-lib/src/main/java/org/x2b/{study => studi}/core/ServiceConstants.java (94%) rename core-service-lib/src/main/java/org/x2b/{study => studi}/core/graphql/errors/UnauthorizedException.java (94%) rename core-service-lib/src/main/java/org/x2b/{study => studi}/core/graphql/fetchers/SecureRootFetcher.java (88%) rename core-service-lib/src/main/java/org/x2b/{study => studi}/core/graphql/util/GraphQLUtils.java (94%) rename core-service-lib/src/main/java/org/x2b/{study => studi}/core/security/User.java (65%) rename core-service-lib/src/main/java/org/x2b/{study => studi}/core/security/data/mongodb/AuthenticatedUser.java (87%) rename core-service-lib/src/main/java/org/x2b/{study => studi}/core/security/data/mongodb/AuthorizationRepository.java (82%) rename core-service-lib/src/main/java/org/x2b/{study => studi}/core/security/jwt/JWTUserTokenVerifier.java (55%) create mode 100644 core-service-lib/src/main/java/org/x2b/studi/core/security/jwt/SharedSecretProvider.java rename core-service-lib/src/main/java/org/x2b/{study => studi}/core/security/shiro/GenericAuthenticatingRealm.java (86%) rename core-service-lib/src/main/java/org/x2b/{study => studi}/core/security/shiro/JWTAuthenticationToken.java (94%) delete mode 100644 core-service-lib/src/main/java/org/x2b/study/core/security/jwt/KeyProviderBean.java rename core-service-lib/src/test/java/org/x2b/{study => studi}/core/TestGraphQLServiceConfigure.java (98%) diff --git a/core-service-lib-integration/src/main/java/org/x2b/study/core/IntegrationTestService.java b/core-service-lib-integration/src/main/java/org/x2b/studi/core/IntegrationTestService.java similarity index 59% rename from core-service-lib-integration/src/main/java/org/x2b/study/core/IntegrationTestService.java rename to core-service-lib-integration/src/main/java/org/x2b/studi/core/IntegrationTestService.java index e153b07..65e8f0e 100644 --- a/core-service-lib-integration/src/main/java/org/x2b/study/core/IntegrationTestService.java +++ b/core-service-lib-integration/src/main/java/org/x2b/studi/core/IntegrationTestService.java @@ -1,16 +1,18 @@ -package org.x2b.study.core; +package org.x2b.studi.core; import graphql.schema.idl.RuntimeWiring; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cache.annotation.EnableCaching; -import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.ComponentScan; -import org.x2b.study.core.graphql.fetchers.mutation.createuser.CreateUserFetcher; -import org.x2b.study.core.graphql.fetchers.mutation.getsecure.SecureTestFetcher; -import org.x2b.study.core.graphql.fetchers.query.getuser.GetUserFetcher; -import org.x2b.study.core.security.data.mongodb.AuthorizationRepository; +import org.x2b.studi.core.graphql.fetchers.mutation.createuser.CreateUserFetcher; +import org.x2b.studi.core.graphql.fetchers.mutation.getsecure.SecureTestFetcher; +import org.x2b.studi.core.graphql.fetchers.query.getuser.GetUserFetcher; +import org.x2b.studi.core.security.data.mongodb.AuthorizationRepository; + +import java.io.IOException; +import java.security.SecureRandom; +import java.util.Base64; @SpringBootApplication @ComponentScan(basePackages = "org.x2b.study.core.*") @@ -18,9 +20,12 @@ public class IntegrationTestService extends GraphQLServiceConfigure { - public static void main(String[] args) { - ConfigurableApplicationContext ctx = SpringApplication.run(IntegrationTestService.class, args); - System.out.println(ctx.getBean("authenticatingRealm")); + public static void main(String[] args) throws IOException { + //ConfigurableApplicationContext ctx = SpringApplication.run(IntegrationTestService.class, args); + SecureRandom random = new SecureRandom(); + byte[] bytes = new byte[32]; + random.nextBytes(bytes); + System.out.println(Base64.getEncoder().encodeToString(bytes)); } @Autowired diff --git a/core-service-lib-integration/src/main/java/org/x2b/study/core/graphql/fetchers/mutation/createuser/CreateUserFetcher.java b/core-service-lib-integration/src/main/java/org/x2b/studi/core/graphql/fetchers/mutation/createuser/CreateUserFetcher.java similarity index 81% rename from core-service-lib-integration/src/main/java/org/x2b/study/core/graphql/fetchers/mutation/createuser/CreateUserFetcher.java rename to core-service-lib-integration/src/main/java/org/x2b/studi/core/graphql/fetchers/mutation/createuser/CreateUserFetcher.java index 4c68e22..f231122 100644 --- a/core-service-lib-integration/src/main/java/org/x2b/study/core/graphql/fetchers/mutation/createuser/CreateUserFetcher.java +++ b/core-service-lib-integration/src/main/java/org/x2b/studi/core/graphql/fetchers/mutation/createuser/CreateUserFetcher.java @@ -1,9 +1,9 @@ -package org.x2b.study.core.graphql.fetchers.mutation.createuser; +package org.x2b.studi.core.graphql.fetchers.mutation.createuser; import graphql.schema.DataFetcher; import graphql.schema.DataFetchingEnvironment; -import org.x2b.study.core.security.data.mongodb.AuthenticatedUser; -import org.x2b.study.core.security.data.mongodb.AuthorizationRepository; +import org.x2b.studi.core.security.data.mongodb.AuthenticatedUser; +import org.x2b.studi.core.security.data.mongodb.AuthorizationRepository; import java.util.HashMap; import java.util.HashSet; diff --git a/core-service-lib-integration/src/main/java/org/x2b/study/core/graphql/fetchers/mutation/getsecure/SecureTestFetcher.java b/core-service-lib-integration/src/main/java/org/x2b/studi/core/graphql/fetchers/mutation/getsecure/SecureTestFetcher.java similarity index 85% rename from core-service-lib-integration/src/main/java/org/x2b/study/core/graphql/fetchers/mutation/getsecure/SecureTestFetcher.java rename to core-service-lib-integration/src/main/java/org/x2b/studi/core/graphql/fetchers/mutation/getsecure/SecureTestFetcher.java index c10aa37..63f04bf 100644 --- a/core-service-lib-integration/src/main/java/org/x2b/study/core/graphql/fetchers/mutation/getsecure/SecureTestFetcher.java +++ b/core-service-lib-integration/src/main/java/org/x2b/studi/core/graphql/fetchers/mutation/getsecure/SecureTestFetcher.java @@ -1,4 +1,4 @@ -package org.x2b.study.core.graphql.fetchers.mutation.getsecure; +package org.x2b.studi.core.graphql.fetchers.mutation.getsecure; import graphql.schema.DataFetchingEnvironment; import org.apache.commons.logging.Log; @@ -6,7 +6,7 @@ import org.apache.shiro.SecurityUtils; import org.apache.shiro.authc.AuthenticationException; import org.apache.shiro.subject.Subject; -import org.x2b.study.core.graphql.fetchers.SecureRootFetcher; +import org.x2b.studi.core.graphql.fetchers.SecureRootFetcher; public class SecureTestFetcher extends SecureRootFetcher { private static final Log log = LogFactory.getLog(SecureTestFetcher.class); diff --git a/core-service-lib-integration/src/main/java/org/x2b/study/core/graphql/fetchers/query/getuser/GetUserFetcher.java b/core-service-lib-integration/src/main/java/org/x2b/studi/core/graphql/fetchers/query/getuser/GetUserFetcher.java similarity index 78% rename from core-service-lib-integration/src/main/java/org/x2b/study/core/graphql/fetchers/query/getuser/GetUserFetcher.java rename to core-service-lib-integration/src/main/java/org/x2b/studi/core/graphql/fetchers/query/getuser/GetUserFetcher.java index 1e2d2d9..aab486e 100644 --- a/core-service-lib-integration/src/main/java/org/x2b/study/core/graphql/fetchers/query/getuser/GetUserFetcher.java +++ b/core-service-lib-integration/src/main/java/org/x2b/studi/core/graphql/fetchers/query/getuser/GetUserFetcher.java @@ -1,9 +1,9 @@ -package org.x2b.study.core.graphql.fetchers.query.getuser; +package org.x2b.studi.core.graphql.fetchers.query.getuser; import graphql.schema.DataFetcher; import graphql.schema.DataFetchingEnvironment; -import org.x2b.study.core.security.data.mongodb.AuthenticatedUser; -import org.x2b.study.core.security.data.mongodb.AuthorizationRepository; +import org.x2b.studi.core.security.data.mongodb.AuthenticatedUser; +import org.x2b.studi.core.security.data.mongodb.AuthorizationRepository; import java.util.ArrayList; import java.util.List; diff --git a/core-service-lib-integration/src/test/java/org/x2b/studi/core/TestIntegrationService.java b/core-service-lib-integration/src/test/java/org/x2b/studi/core/TestIntegrationService.java index 8d2e132..889f527 100644 --- a/core-service-lib-integration/src/test/java/org/x2b/studi/core/TestIntegrationService.java +++ b/core-service-lib-integration/src/test/java/org/x2b/studi/core/TestIntegrationService.java @@ -15,7 +15,6 @@ import org.springframework.test.web.servlet.setup.DefaultMockMvcBuilder; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.web.context.WebApplicationContext; -import org.x2b.study.core.IntegrationTestService; @RunWith(SpringJUnit4ClassRunner.class) @WebAppConfiguration diff --git a/core-service-lib/src/main/java/org/x2b/study/core/GraphQLServiceConfigure.java b/core-service-lib/src/main/java/org/x2b/studi/core/GraphQLServiceConfigure.java similarity index 85% rename from core-service-lib/src/main/java/org/x2b/study/core/GraphQLServiceConfigure.java rename to core-service-lib/src/main/java/org/x2b/studi/core/GraphQLServiceConfigure.java index 04ef26c..ddbba51 100644 --- a/core-service-lib/src/main/java/org/x2b/study/core/GraphQLServiceConfigure.java +++ b/core-service-lib/src/main/java/org/x2b/studi/core/GraphQLServiceConfigure.java @@ -1,4 +1,4 @@ -package org.x2b.study.core; +package org.x2b.studi.core; import graphql.schema.GraphQLSchema; @@ -10,28 +10,25 @@ import org.apache.shiro.mgt.DefaultSubjectDAO; import org.apache.shiro.mgt.SubjectDAO; import org.apache.shiro.spring.web.ShiroFilterFactoryBean; -import org.apache.shiro.web.filter.session.NoSessionCreationFilter; import org.apache.shiro.web.mgt.DefaultWebSecurityManager; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.web.servlet.FilterRegistrationBean; -import org.springframework.cache.annotation.EnableCaching; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.web.filter.DelegatingFilterProxy; -import org.x2b.study.core.security.jwt.JWTUserTokenVerifier; -import org.x2b.study.core.security.jwt.KeyProviderBean; -import org.x2b.study.core.security.shiro.GenericAuthenticatingRealm; +import org.x2b.studi.core.security.jwt.JWTUserTokenVerifier; +import org.x2b.studi.core.security.jwt.SharedSecretProvider; +import org.x2b.studi.core.security.shiro.GenericAuthenticatingRealm; import java.io.File; -import java.io.IOException; -import java.security.GeneralSecurityException; public abstract class GraphQLServiceConfigure { @Value("#{graphql.schema.schemaFileLocation}") public static String schemaFileLocation = "schema.gql"; + @Autowired protected ApplicationContext applicationContext; @@ -89,13 +86,13 @@ public ShiroFilterFactoryBean shiroFilter() { } @Bean - public KeyProviderBean jwtAuthKeyProvider() { - return new KeyProviderBean(); + public SharedSecretProvider jwtAuthKeyProvider() { + return new SharedSecretProvider(); } @Bean public JWTUserTokenVerifier jwtUserTokenVerifier() { - return new JWTUserTokenVerifier((KeyProviderBean) applicationContext.getBean("jwtAuthKeyProvider")); + return new JWTUserTokenVerifier((SharedSecretProvider) applicationContext.getBean("jwtAuthKeyProvider")); } private File getSchemaFile() { diff --git a/core-service-lib/src/main/java/org/x2b/study/core/ServiceConstants.java b/core-service-lib/src/main/java/org/x2b/studi/core/ServiceConstants.java similarity index 94% rename from core-service-lib/src/main/java/org/x2b/study/core/ServiceConstants.java rename to core-service-lib/src/main/java/org/x2b/studi/core/ServiceConstants.java index cdbf112..e3dae08 100644 --- a/core-service-lib/src/main/java/org/x2b/study/core/ServiceConstants.java +++ b/core-service-lib/src/main/java/org/x2b/studi/core/ServiceConstants.java @@ -1,4 +1,4 @@ -package org.x2b.study.core; +package org.x2b.studi.core; public final class ServiceConstants { diff --git a/core-service-lib/src/main/java/org/x2b/study/core/graphql/errors/UnauthorizedException.java b/core-service-lib/src/main/java/org/x2b/studi/core/graphql/errors/UnauthorizedException.java similarity index 94% rename from core-service-lib/src/main/java/org/x2b/study/core/graphql/errors/UnauthorizedException.java rename to core-service-lib/src/main/java/org/x2b/studi/core/graphql/errors/UnauthorizedException.java index 7b2acb3..f9f1819 100644 --- a/core-service-lib/src/main/java/org/x2b/study/core/graphql/errors/UnauthorizedException.java +++ b/core-service-lib/src/main/java/org/x2b/studi/core/graphql/errors/UnauthorizedException.java @@ -1,4 +1,4 @@ -package org.x2b.study.core.graphql.errors; +package org.x2b.studi.core.graphql.errors; import graphql.ErrorType; import graphql.GraphQLError; diff --git a/core-service-lib/src/main/java/org/x2b/study/core/graphql/fetchers/SecureRootFetcher.java b/core-service-lib/src/main/java/org/x2b/studi/core/graphql/fetchers/SecureRootFetcher.java similarity index 88% rename from core-service-lib/src/main/java/org/x2b/study/core/graphql/fetchers/SecureRootFetcher.java rename to core-service-lib/src/main/java/org/x2b/studi/core/graphql/fetchers/SecureRootFetcher.java index 52cb6ed..c87c553 100644 --- a/core-service-lib/src/main/java/org/x2b/study/core/graphql/fetchers/SecureRootFetcher.java +++ b/core-service-lib/src/main/java/org/x2b/studi/core/graphql/fetchers/SecureRootFetcher.java @@ -1,4 +1,4 @@ -package org.x2b.study.core.graphql.fetchers; +package org.x2b.studi.core.graphql.fetchers; import graphql.schema.DataFetcher; import graphql.schema.DataFetchingEnvironment; @@ -8,9 +8,9 @@ import org.apache.shiro.mgt.RealmSecurityManager; import org.apache.shiro.realm.Realm; import org.apache.shiro.subject.Subject; -import org.x2b.study.core.ServiceConstants; -import org.x2b.study.core.graphql.util.GraphQLUtils; -import org.x2b.study.core.security.shiro.JWTAuthenticationToken; +import org.x2b.studi.core.graphql.util.GraphQLUtils; +import org.x2b.studi.core.ServiceConstants; +import org.x2b.studi.core.security.shiro.JWTAuthenticationToken; /** * Provides a secure edge for GraphQL queries. User this for all root query types to ensure that the user is properly diff --git a/core-service-lib/src/main/java/org/x2b/study/core/graphql/util/GraphQLUtils.java b/core-service-lib/src/main/java/org/x2b/studi/core/graphql/util/GraphQLUtils.java similarity index 94% rename from core-service-lib/src/main/java/org/x2b/study/core/graphql/util/GraphQLUtils.java rename to core-service-lib/src/main/java/org/x2b/studi/core/graphql/util/GraphQLUtils.java index 6db6fc0..9413ad3 100644 --- a/core-service-lib/src/main/java/org/x2b/study/core/graphql/util/GraphQLUtils.java +++ b/core-service-lib/src/main/java/org/x2b/studi/core/graphql/util/GraphQLUtils.java @@ -1,4 +1,4 @@ -package org.x2b.study.core.graphql.util; +package org.x2b.studi.core.graphql.util; import graphql.schema.DataFetchingEnvironment; import graphql.servlet.GraphQLContext; diff --git a/core-service-lib/src/main/java/org/x2b/study/core/security/User.java b/core-service-lib/src/main/java/org/x2b/studi/core/security/User.java similarity index 65% rename from core-service-lib/src/main/java/org/x2b/study/core/security/User.java rename to core-service-lib/src/main/java/org/x2b/studi/core/security/User.java index e3ea060..f540b46 100644 --- a/core-service-lib/src/main/java/org/x2b/study/core/security/User.java +++ b/core-service-lib/src/main/java/org/x2b/studi/core/security/User.java @@ -1,4 +1,4 @@ -package org.x2b.study.core.security; +package org.x2b.studi.core.security; import java.util.UUID; diff --git a/core-service-lib/src/main/java/org/x2b/study/core/security/data/mongodb/AuthenticatedUser.java b/core-service-lib/src/main/java/org/x2b/studi/core/security/data/mongodb/AuthenticatedUser.java similarity index 87% rename from core-service-lib/src/main/java/org/x2b/study/core/security/data/mongodb/AuthenticatedUser.java rename to core-service-lib/src/main/java/org/x2b/studi/core/security/data/mongodb/AuthenticatedUser.java index 952d264..d1c9245 100644 --- a/core-service-lib/src/main/java/org/x2b/study/core/security/data/mongodb/AuthenticatedUser.java +++ b/core-service-lib/src/main/java/org/x2b/studi/core/security/data/mongodb/AuthenticatedUser.java @@ -1,13 +1,13 @@ -package org.x2b.study.core.security.data.mongodb; +package org.x2b.studi.core.security.data.mongodb; import org.springframework.data.annotation.Id; -import org.x2b.study.core.security.User; +import org.x2b.studi.core.security.User; import java.util.HashSet; import java.util.Set; import java.util.UUID; -public class AuthenticatedUser implements User{ +public class AuthenticatedUser implements User { @Id private final UUID id; diff --git a/core-service-lib/src/main/java/org/x2b/study/core/security/data/mongodb/AuthorizationRepository.java b/core-service-lib/src/main/java/org/x2b/studi/core/security/data/mongodb/AuthorizationRepository.java similarity index 82% rename from core-service-lib/src/main/java/org/x2b/study/core/security/data/mongodb/AuthorizationRepository.java rename to core-service-lib/src/main/java/org/x2b/studi/core/security/data/mongodb/AuthorizationRepository.java index 60ad9d2..74ac425 100644 --- a/core-service-lib/src/main/java/org/x2b/study/core/security/data/mongodb/AuthorizationRepository.java +++ b/core-service-lib/src/main/java/org/x2b/studi/core/security/data/mongodb/AuthorizationRepository.java @@ -1,11 +1,9 @@ -package org.x2b.study.core.security.data.mongodb; +package org.x2b.studi.core.security.data.mongodb; import org.springframework.cache.annotation.CacheEvict; import org.springframework.cache.annotation.Cacheable; -import org.springframework.data.domain.Example; import org.springframework.data.mongodb.repository.MongoRepository; -import java.util.List; import java.util.UUID; public interface AuthorizationRepository extends MongoRepository { diff --git a/core-service-lib/src/main/java/org/x2b/study/core/security/jwt/JWTUserTokenVerifier.java b/core-service-lib/src/main/java/org/x2b/studi/core/security/jwt/JWTUserTokenVerifier.java similarity index 55% rename from core-service-lib/src/main/java/org/x2b/study/core/security/jwt/JWTUserTokenVerifier.java rename to core-service-lib/src/main/java/org/x2b/studi/core/security/jwt/JWTUserTokenVerifier.java index cc9b01c..29149c9 100644 --- a/core-service-lib/src/main/java/org/x2b/study/core/security/jwt/JWTUserTokenVerifier.java +++ b/core-service-lib/src/main/java/org/x2b/studi/core/security/jwt/JWTUserTokenVerifier.java @@ -1,21 +1,14 @@ -package org.x2b.study.core.security.jwt; +package org.x2b.studi.core.security.jwt; import com.auth0.jwt.JWT; import com.auth0.jwt.JWTVerifier; import com.auth0.jwt.algorithms.Algorithm; -import com.auth0.jwt.interfaces.RSAKeyProvider; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Qualifier; -import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; -import org.x2b.study.core.ServiceConstants; -import org.x2b.study.core.security.User; -import org.x2b.study.core.security.shiro.JWTAuthenticationToken; -import sun.security.rsa.RSAPublicKeyImpl; - -import java.io.UnsupportedEncodingException; -import java.security.interfaces.RSAPrivateKey; -import java.security.interfaces.RSAPublicKey; +import org.x2b.studi.core.security.User; +import org.x2b.studi.core.ServiceConstants; +import org.x2b.studi.core.security.shiro.JWTAuthenticationToken; + import java.util.UUID; @Component @@ -24,10 +17,9 @@ public class JWTUserTokenVerifier { private final JWTVerifier verifier; @Autowired - public JWTUserTokenVerifier(KeyProviderBean rsaKeyProvider) { - RSAPublicKey publicKey = rsaKeyProvider.getPublicKey(); - RSAPrivateKey privateKey = rsaKeyProvider.getPrivateKey(); - Algorithm algorithm = Algorithm.RSA256(publicKey, privateKey); + public JWTUserTokenVerifier(SharedSecretProvider authSecretProvider) { + + Algorithm algorithm = Algorithm.HMAC256(authSecretProvider.getKey()); verifier = JWT.require(algorithm) .withIssuer(ServiceConstants.SECURITY_TOKEN_ISSUER) .build(); diff --git a/core-service-lib/src/main/java/org/x2b/studi/core/security/jwt/SharedSecretProvider.java b/core-service-lib/src/main/java/org/x2b/studi/core/security/jwt/SharedSecretProvider.java new file mode 100644 index 0000000..13cc47c --- /dev/null +++ b/core-service-lib/src/main/java/org/x2b/studi/core/security/jwt/SharedSecretProvider.java @@ -0,0 +1,21 @@ +package org.x2b.studi.core.security.jwt; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; + +import java.util.Base64; + +/** + * Reads the shared secret from the proper location + */ +//TODO: not sure this really needs to be it's own class +@Component +public class SharedSecretProvider { + + @Value("${studi.security.auth.secret}") + private String encodedKey; + + public byte[] getKey() { + return Base64.getDecoder().decode(encodedKey); //I think this won't be a performance problem + } +} diff --git a/core-service-lib/src/main/java/org/x2b/study/core/security/shiro/GenericAuthenticatingRealm.java b/core-service-lib/src/main/java/org/x2b/studi/core/security/shiro/GenericAuthenticatingRealm.java similarity index 86% rename from core-service-lib/src/main/java/org/x2b/study/core/security/shiro/GenericAuthenticatingRealm.java rename to core-service-lib/src/main/java/org/x2b/studi/core/security/shiro/GenericAuthenticatingRealm.java index adc564b..bd429f3 100644 --- a/core-service-lib/src/main/java/org/x2b/study/core/security/shiro/GenericAuthenticatingRealm.java +++ b/core-service-lib/src/main/java/org/x2b/studi/core/security/shiro/GenericAuthenticatingRealm.java @@ -1,4 +1,4 @@ -package org.x2b.study.core.security.shiro; +package org.x2b.studi.core.security.shiro; import org.apache.shiro.authc.AuthenticationException; import org.apache.shiro.authc.AuthenticationInfo; @@ -10,11 +10,11 @@ import org.apache.shiro.subject.PrincipalCollection; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; -import org.x2b.study.core.ServiceConstants; -import org.x2b.study.core.security.User; -import org.x2b.study.core.security.data.mongodb.AuthenticatedUser; -import org.x2b.study.core.security.data.mongodb.AuthorizationRepository; -import org.x2b.study.core.security.jwt.JWTUserTokenVerifier; +import org.x2b.studi.core.security.User; +import org.x2b.studi.core.ServiceConstants; +import org.x2b.studi.core.security.data.mongodb.AuthenticatedUser; +import org.x2b.studi.core.security.data.mongodb.AuthorizationRepository; +import org.x2b.studi.core.security.jwt.JWTUserTokenVerifier; @Component public class GenericAuthenticatingRealm extends AuthorizingRealm { diff --git a/core-service-lib/src/main/java/org/x2b/study/core/security/shiro/JWTAuthenticationToken.java b/core-service-lib/src/main/java/org/x2b/studi/core/security/shiro/JWTAuthenticationToken.java similarity index 94% rename from core-service-lib/src/main/java/org/x2b/study/core/security/shiro/JWTAuthenticationToken.java rename to core-service-lib/src/main/java/org/x2b/studi/core/security/shiro/JWTAuthenticationToken.java index a77ada4..fff1538 100644 --- a/core-service-lib/src/main/java/org/x2b/study/core/security/shiro/JWTAuthenticationToken.java +++ b/core-service-lib/src/main/java/org/x2b/studi/core/security/shiro/JWTAuthenticationToken.java @@ -1,4 +1,4 @@ -package org.x2b.study.core.security.shiro; +package org.x2b.studi.core.security.shiro; import org.apache.shiro.authc.AuthenticationToken; diff --git a/core-service-lib/src/main/java/org/x2b/study/core/security/jwt/KeyProviderBean.java b/core-service-lib/src/main/java/org/x2b/study/core/security/jwt/KeyProviderBean.java deleted file mode 100644 index 33a13f6..0000000 --- a/core-service-lib/src/main/java/org/x2b/study/core/security/jwt/KeyProviderBean.java +++ /dev/null @@ -1,86 +0,0 @@ -package org.x2b.study.core.security.jwt; - -import org.apache.shiro.codec.Base64; -import org.bouncycastle.crypto.params.AsymmetricKeyParameter; -import org.bouncycastle.crypto.params.ECKeyParameters; -import org.bouncycastle.crypto.util.PrivateKeyFactory; -import org.springframework.beans.factory.annotation.Value; -import org.springframework.stereotype.Component; - -import javax.annotation.PostConstruct; -import java.io.ByteArrayInputStream; -import java.io.IOException; -import java.io.UnsupportedEncodingException; -import java.security.*; -import java.security.cert.CertificateException; -import java.security.cert.CertificateFactory; -import java.security.cert.X509Certificate; -import java.security.interfaces.RSAPrivateKey; -import java.security.interfaces.RSAPublicKey; -import java.security.spec.InvalidKeySpecException; -import java.security.spec.PKCS8EncodedKeySpec; -import java.security.spec.X509EncodedKeySpec; - -@Component -public class KeyProviderBean { - - @Value("${auth.rsa.public-key}") - private String encodedPublicKey; - - @Value("${auth.rsa.private-key}") - private String encodedPrivateKey; - - private RSAPublicKey publicKey; - private RSAPrivateKey privateKey; - - - @PostConstruct //TODO: I hate this - private void setUpKeys() throws IOException, GeneralSecurityException { - KeyFactory rsaKeyFactory = KeyFactory.getInstance("RSA"); - publicKey = getPublicKey(encodedPublicKey, rsaKeyFactory); - privateKey = getPrivateKey(encodedPrivateKey, rsaKeyFactory); - System.out.println(publicKey.getAlgorithm()); - System.out.println(privateKey.getAlgorithm()); - } - - private RSAPublicKey getPublicKey(final String publicKeyStr, final KeyFactory keyFactory) - throws IOException, InvalidKeySpecException, CertificateException, NoSuchProviderException { - if (publicKeyStr == null) { - return null; - } - CertificateFactory cf = CertificateFactory.getInstance("X509", "BC"); - try (ByteArrayInputStream bis = new ByteArrayInputStream(publicKeyStr.getBytes())) { - X509Certificate cert = (X509Certificate) cf.generateCertificate(bis); - return (RSAPublicKey) cert.getPublicKey(); - } - } - - private RSAPrivateKey getPrivateKey(final String privateKeyStr, final KeyFactory keyFactory) - throws IOException, InvalidKeySpecException, CertificateException, NoSuchProviderException { - if (privateKeyStr == null) { - return null; - } - return null; - } - - private byte[] getKeyBytes(final String key) throws UnsupportedEncodingException { - return Base64.decode(key.getBytes("utf-8")); - } - - /** - * Get this provider's public key - * @return null iff a public key is not defined - */ - public RSAPublicKey getPublicKey() { - return publicKey; - } - - /** - * Get this providers private key - * @return null iff a private key is not defined - */ - public RSAPrivateKey getPrivateKey() { - return privateKey; - } - -} diff --git a/core-service-lib/src/test/java/org/x2b/study/core/TestGraphQLServiceConfigure.java b/core-service-lib/src/test/java/org/x2b/studi/core/TestGraphQLServiceConfigure.java similarity index 98% rename from core-service-lib/src/test/java/org/x2b/study/core/TestGraphQLServiceConfigure.java rename to core-service-lib/src/test/java/org/x2b/studi/core/TestGraphQLServiceConfigure.java index bba56f9..0695d59 100644 --- a/core-service-lib/src/test/java/org/x2b/study/core/TestGraphQLServiceConfigure.java +++ b/core-service-lib/src/test/java/org/x2b/studi/core/TestGraphQLServiceConfigure.java @@ -1,4 +1,4 @@ -package org.x2b.study.core; +package org.x2b.studi.core; import graphql.schema.GraphQLSchema; import graphql.schema.GraphQLType; From 684ea0cd4c68e5547dab6c1a2f05bbff7aaf6d08 Mon Sep 17 00:00:00 2001 From: Ethan Hunter Date: Sat, 21 Oct 2017 01:40:08 -0600 Subject: [PATCH 39/47] clean up code and test signed tokens --- .../org/x2b/studi/core/IntegrationTestService.java | 8 +++----- .../core/security/jwt/JWTUserTokenVerifier.java | 12 +++++++----- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/core-service-lib-integration/src/main/java/org/x2b/studi/core/IntegrationTestService.java b/core-service-lib-integration/src/main/java/org/x2b/studi/core/IntegrationTestService.java index 65e8f0e..234ea59 100644 --- a/core-service-lib-integration/src/main/java/org/x2b/studi/core/IntegrationTestService.java +++ b/core-service-lib-integration/src/main/java/org/x2b/studi/core/IntegrationTestService.java @@ -2,8 +2,10 @@ import graphql.schema.idl.RuntimeWiring; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cache.annotation.EnableCaching; +import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.ComponentScan; import org.x2b.studi.core.graphql.fetchers.mutation.createuser.CreateUserFetcher; import org.x2b.studi.core.graphql.fetchers.mutation.getsecure.SecureTestFetcher; @@ -21,11 +23,7 @@ public class IntegrationTestService extends GraphQLServiceConfigure { public static void main(String[] args) throws IOException { - //ConfigurableApplicationContext ctx = SpringApplication.run(IntegrationTestService.class, args); - SecureRandom random = new SecureRandom(); - byte[] bytes = new byte[32]; - random.nextBytes(bytes); - System.out.println(Base64.getEncoder().encodeToString(bytes)); + ConfigurableApplicationContext ctx = SpringApplication.run(IntegrationTestService.class, args); } @Autowired diff --git a/core-service-lib/src/main/java/org/x2b/studi/core/security/jwt/JWTUserTokenVerifier.java b/core-service-lib/src/main/java/org/x2b/studi/core/security/jwt/JWTUserTokenVerifier.java index 29149c9..5f46388 100644 --- a/core-service-lib/src/main/java/org/x2b/studi/core/security/jwt/JWTUserTokenVerifier.java +++ b/core-service-lib/src/main/java/org/x2b/studi/core/security/jwt/JWTUserTokenVerifier.java @@ -3,6 +3,8 @@ import com.auth0.jwt.JWT; import com.auth0.jwt.JWTVerifier; import com.auth0.jwt.algorithms.Algorithm; +import com.auth0.jwt.interfaces.Claim; +import com.auth0.jwt.interfaces.DecodedJWT; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.x2b.studi.core.security.User; @@ -27,11 +29,11 @@ public JWTUserTokenVerifier(SharedSecretProvider authSecretProvider) { public User getUser(JWTAuthenticationToken token) { //TODO: Eventually this should use getSubject to get a JSON and then deserialize into java -// DecodedJWT decodedJWT = verifier.verify(token.getToken()); -// Claim uuidClaim = decodedJWT.getClaim(ServiceConstants.SECURITY_UUID_CLAIM); -// String uuidString = uuidClaim.asString(); -// UUID uuid = UUID.fromString(uuidString); + DecodedJWT decodedJWT = verifier.verify(token.getToken()); + Claim uuidClaim = decodedJWT.getClaim(ServiceConstants.SECURITY_UUID_CLAIM); + String uuidString = uuidClaim.asString(); + UUID uuid = UUID.fromString(uuidString); - return () -> UUID.fromString(token.getToken()); //TODO: this is not a good place for a lambda + return () -> uuid; //TODO: this is not a good place for a lambda } } From 2544b50c3e28cea0d63244dc77c85d7ffc3d6d7a Mon Sep 17 00:00:00 2001 From: Ethan Hunter Date: Sat, 21 Oct 2017 13:23:51 -0600 Subject: [PATCH 40/47] fix unit tests --- .../test/java/org/x2b/studi/core/TestIntegrationService.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/core-service-lib-integration/src/test/java/org/x2b/studi/core/TestIntegrationService.java b/core-service-lib-integration/src/test/java/org/x2b/studi/core/TestIntegrationService.java index 889f527..ee9ff06 100644 --- a/core-service-lib-integration/src/test/java/org/x2b/studi/core/TestIntegrationService.java +++ b/core-service-lib-integration/src/test/java/org/x2b/studi/core/TestIntegrationService.java @@ -5,6 +5,7 @@ import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.TestPropertySource; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.web.WebAppConfiguration; import org.springframework.test.web.servlet.MockMvc; @@ -19,6 +20,9 @@ @RunWith(SpringJUnit4ClassRunner.class) @WebAppConfiguration @ContextConfiguration(classes = IntegrationTestService.class) +@TestPropertySource(properties = { + "studi.security.auth.secret=abcdefg123" +}) public class TestIntegrationService { @Autowired From d0a8af72ec69485198d42f76198779e13569972a Mon Sep 17 00:00:00 2001 From: Ethan Hunter Date: Sat, 21 Oct 2017 21:30:22 -0600 Subject: [PATCH 41/47] fix build --- .../studi/core/security/shiro/GenericAuthenticatingRealm.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core-service-lib/src/main/java/org/x2b/studi/core/security/shiro/GenericAuthenticatingRealm.java b/core-service-lib/src/main/java/org/x2b/studi/core/security/shiro/GenericAuthenticatingRealm.java index bd429f3..b6a81f4 100644 --- a/core-service-lib/src/main/java/org/x2b/studi/core/security/shiro/GenericAuthenticatingRealm.java +++ b/core-service-lib/src/main/java/org/x2b/studi/core/security/shiro/GenericAuthenticatingRealm.java @@ -11,10 +11,10 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import org.x2b.studi.core.security.User; -import org.x2b.studi.core.ServiceConstants; import org.x2b.studi.core.security.data.mongodb.AuthenticatedUser; import org.x2b.studi.core.security.data.mongodb.AuthorizationRepository; import org.x2b.studi.core.security.jwt.JWTUserTokenVerifier; +import org.x2b.studi.core.ServiceConstants; @Component public class GenericAuthenticatingRealm extends AuthorizingRealm { From 11f02ec78441bc6abbcfee59dc8521f79cf69010 Mon Sep 17 00:00:00 2001 From: Ethan Hunter Date: Sat, 21 Oct 2017 21:42:39 -0600 Subject: [PATCH 42/47] add delete method to Auth repo --- .../core/security/data/mongodb/AuthorizationRepository.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/core-service-lib/src/main/java/org/x2b/studi/core/security/data/mongodb/AuthorizationRepository.java b/core-service-lib/src/main/java/org/x2b/studi/core/security/data/mongodb/AuthorizationRepository.java index 74ac425..d36ff0d 100644 --- a/core-service-lib/src/main/java/org/x2b/studi/core/security/data/mongodb/AuthorizationRepository.java +++ b/core-service-lib/src/main/java/org/x2b/studi/core/security/data/mongodb/AuthorizationRepository.java @@ -18,4 +18,8 @@ public interface AuthorizationRepository extends MongoRepository Date: Sat, 21 Oct 2017 21:44:06 -0600 Subject: [PATCH 43/47] add insert method to auth repo --- .../core/security/data/mongodb/AuthorizationRepository.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/core-service-lib/src/main/java/org/x2b/studi/core/security/data/mongodb/AuthorizationRepository.java b/core-service-lib/src/main/java/org/x2b/studi/core/security/data/mongodb/AuthorizationRepository.java index d36ff0d..2bb0d2c 100644 --- a/core-service-lib/src/main/java/org/x2b/studi/core/security/data/mongodb/AuthorizationRepository.java +++ b/core-service-lib/src/main/java/org/x2b/studi/core/security/data/mongodb/AuthorizationRepository.java @@ -19,6 +19,10 @@ public interface AuthorizationRepository extends MongoRepository Date: Sat, 21 Oct 2017 21:50:56 -0600 Subject: [PATCH 44/47] add mongo config methods --- .../java/org/x2b/studi/core/IntegrationTestService.java | 9 +++++++++ .../java/org/x2b/studi/core/GraphQLServiceConfigure.java | 8 ++++++++ 2 files changed, 17 insertions(+) diff --git a/core-service-lib-integration/src/main/java/org/x2b/studi/core/IntegrationTestService.java b/core-service-lib-integration/src/main/java/org/x2b/studi/core/IntegrationTestService.java index 234ea59..cb1e1c5 100644 --- a/core-service-lib-integration/src/main/java/org/x2b/studi/core/IntegrationTestService.java +++ b/core-service-lib-integration/src/main/java/org/x2b/studi/core/IntegrationTestService.java @@ -7,6 +7,7 @@ import org.springframework.cache.annotation.EnableCaching; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.ComponentScan; +import org.springframework.data.mongodb.core.MongoClientFactoryBean; import org.x2b.studi.core.graphql.fetchers.mutation.createuser.CreateUserFetcher; import org.x2b.studi.core.graphql.fetchers.mutation.getsecure.SecureTestFetcher; import org.x2b.studi.core.graphql.fetchers.query.getuser.GetUserFetcher; @@ -29,6 +30,14 @@ public static void main(String[] args) throws IOException { @Autowired public AuthorizationRepository authRepo; + @Override + protected MongoClientFactoryBean createAuthDatasourceFactory() { + MongoClientFactoryBean factoryBean = new MongoClientFactoryBean(); + factoryBean.setHost("localhost"); + factoryBean.setPort(27017); + return factoryBean; + } + @Override protected RuntimeWiring createRuntimeWiring() { return RuntimeWiring.newRuntimeWiring() diff --git a/core-service-lib/src/main/java/org/x2b/studi/core/GraphQLServiceConfigure.java b/core-service-lib/src/main/java/org/x2b/studi/core/GraphQLServiceConfigure.java index ddbba51..3a17354 100644 --- a/core-service-lib/src/main/java/org/x2b/studi/core/GraphQLServiceConfigure.java +++ b/core-service-lib/src/main/java/org/x2b/studi/core/GraphQLServiceConfigure.java @@ -16,6 +16,7 @@ import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Bean; +import org.springframework.data.mongodb.core.MongoClientFactoryBean; import org.springframework.web.filter.DelegatingFilterProxy; import org.x2b.studi.core.security.jwt.JWTUserTokenVerifier; import org.x2b.studi.core.security.jwt.SharedSecretProvider; @@ -95,6 +96,13 @@ public JWTUserTokenVerifier jwtUserTokenVerifier() { return new JWTUserTokenVerifier((SharedSecretProvider) applicationContext.getBean("jwtAuthKeyProvider")); } + @Bean + public MongoClientFactoryBean authorizationDatasource() { + return createAuthDatasourceFactory(); + } + + protected abstract MongoClientFactoryBean createAuthDatasourceFactory(); + private File getSchemaFile() { return new File(this.getClass().getClassLoader().getResource(schemaFileLocation).getFile()); } From 47a33513bb79fa01a8386410116c08d4a151f711 Mon Sep 17 00:00:00 2001 From: Ethan Hunter Date: Sat, 21 Oct 2017 21:58:07 -0600 Subject: [PATCH 45/47] heh... fix build --- .../org/x2b/studi/core/TestGraphQLServiceConfigure.java | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/core-service-lib/src/test/java/org/x2b/studi/core/TestGraphQLServiceConfigure.java b/core-service-lib/src/test/java/org/x2b/studi/core/TestGraphQLServiceConfigure.java index 0695d59..b5099c3 100644 --- a/core-service-lib/src/test/java/org/x2b/studi/core/TestGraphQLServiceConfigure.java +++ b/core-service-lib/src/test/java/org/x2b/studi/core/TestGraphQLServiceConfigure.java @@ -5,6 +5,7 @@ import graphql.schema.idl.RuntimeWiring; import org.junit.Assert; import org.junit.Test; +import org.springframework.data.mongodb.core.MongoClientFactoryBean; import java.util.List; @@ -21,6 +22,14 @@ public String getValue() { } } + @Override + protected MongoClientFactoryBean createAuthDatasourceFactory() { + MongoClientFactoryBean factoryBean = new MongoClientFactoryBean(); + factoryBean.setHost("localhost"); + factoryBean.setPort(27017); + return factoryBean; + } + @Override protected RuntimeWiring createRuntimeWiring() { return RuntimeWiring.newRuntimeWiring() From 93b2be24e66e320011dcf467616c830a0b0f3da4 Mon Sep 17 00:00:00 2001 From: Ethan Hunter Date: Sat, 21 Oct 2017 22:20:29 -0600 Subject: [PATCH 46/47] move MongoDB config into config options --- .../studi/core/IntegrationTestService.java | 10 +------- .../studi/core/GraphQLServiceConfigure.java | 23 +++++++++++++----- .../org/x2b/studi/core/ServiceConstants.java | 5 +++- .../data/mongodb/MongoDbDetailsProvider.java | 24 +++++++++++++++++++ .../core/TestGraphQLServiceConfigure.java | 9 +------ 5 files changed, 47 insertions(+), 24 deletions(-) create mode 100644 core-service-lib/src/main/java/org/x2b/studi/core/security/data/mongodb/MongoDbDetailsProvider.java diff --git a/core-service-lib-integration/src/main/java/org/x2b/studi/core/IntegrationTestService.java b/core-service-lib-integration/src/main/java/org/x2b/studi/core/IntegrationTestService.java index cb1e1c5..955a354 100644 --- a/core-service-lib-integration/src/main/java/org/x2b/studi/core/IntegrationTestService.java +++ b/core-service-lib-integration/src/main/java/org/x2b/studi/core/IntegrationTestService.java @@ -28,15 +28,7 @@ public static void main(String[] args) throws IOException { } @Autowired - public AuthorizationRepository authRepo; - - @Override - protected MongoClientFactoryBean createAuthDatasourceFactory() { - MongoClientFactoryBean factoryBean = new MongoClientFactoryBean(); - factoryBean.setHost("localhost"); - factoryBean.setPort(27017); - return factoryBean; - } + private AuthorizationRepository authRepo; @Override protected RuntimeWiring createRuntimeWiring() { diff --git a/core-service-lib/src/main/java/org/x2b/studi/core/GraphQLServiceConfigure.java b/core-service-lib/src/main/java/org/x2b/studi/core/GraphQLServiceConfigure.java index 3a17354..4e3a425 100644 --- a/core-service-lib/src/main/java/org/x2b/studi/core/GraphQLServiceConfigure.java +++ b/core-service-lib/src/main/java/org/x2b/studi/core/GraphQLServiceConfigure.java @@ -16,19 +16,20 @@ import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; import org.springframework.data.mongodb.core.MongoClientFactoryBean; import org.springframework.web.filter.DelegatingFilterProxy; +import org.x2b.studi.core.security.data.mongodb.MongoDbDetailsProvider; import org.x2b.studi.core.security.jwt.JWTUserTokenVerifier; import org.x2b.studi.core.security.jwt.SharedSecretProvider; import org.x2b.studi.core.security.shiro.GenericAuthenticatingRealm; import java.io.File; +@Configuration public abstract class GraphQLServiceConfigure { - @Value("#{graphql.schema.schemaFileLocation}") - public static String schemaFileLocation = "schema.gql"; - + protected String schemaFileLocation = "schema.gql"; @Autowired protected ApplicationContext applicationContext; @@ -96,12 +97,22 @@ public JWTUserTokenVerifier jwtUserTokenVerifier() { return new JWTUserTokenVerifier((SharedSecretProvider) applicationContext.getBean("jwtAuthKeyProvider")); } + @Bean - public MongoClientFactoryBean authorizationDatasource() { - return createAuthDatasourceFactory(); + public MongoDbDetailsProvider authDbDetailsProvider() { + return new MongoDbDetailsProvider(); } - protected abstract MongoClientFactoryBean createAuthDatasourceFactory(); + @Bean + public MongoClientFactoryBean authorizationDatasource() { + MongoClientFactoryBean factory = new MongoClientFactoryBean(); + MongoDbDetailsProvider detailsProvider = authDbDetailsProvider(); + + factory.setHost(detailsProvider.getHost()); + factory.setPort(detailsProvider.getPort()); + + return factory; + } private File getSchemaFile() { return new File(this.getClass().getClassLoader().getResource(schemaFileLocation).getFile()); diff --git a/core-service-lib/src/main/java/org/x2b/studi/core/ServiceConstants.java b/core-service-lib/src/main/java/org/x2b/studi/core/ServiceConstants.java index e3dae08..50b4d1e 100644 --- a/core-service-lib/src/main/java/org/x2b/studi/core/ServiceConstants.java +++ b/core-service-lib/src/main/java/org/x2b/studi/core/ServiceConstants.java @@ -9,7 +9,10 @@ private ServiceConstants() {} public static final String SECURITY_UUID_CLAIM = "uuid"; public static final String SECURITY_AUTHENTICATION_REALM_NAME = "generic_authentication_realm"; + public static final String SECURITY_DATA_MONGODB_HOST_PROPERTY = "security.data.mongodb.host"; + public static final String SECURITY_DATA_MONGODB_PORT_PROPERTY = "security.data.mongodb.port"; + public static final String HTTP_AUTH_HEADER = "Authorization"; - public static final String DO_NOT_USE_THIS_IN_PRODUCTION = "secret"; + } diff --git a/core-service-lib/src/main/java/org/x2b/studi/core/security/data/mongodb/MongoDbDetailsProvider.java b/core-service-lib/src/main/java/org/x2b/studi/core/security/data/mongodb/MongoDbDetailsProvider.java new file mode 100644 index 0000000..242f96a --- /dev/null +++ b/core-service-lib/src/main/java/org/x2b/studi/core/security/data/mongodb/MongoDbDetailsProvider.java @@ -0,0 +1,24 @@ +package org.x2b.studi.core.security.data.mongodb; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.stereotype.Component; +import org.x2b.studi.core.ServiceConstants; + +@Component +public class MongoDbDetailsProvider { + + @Value("${" + ServiceConstants.SECURITY_DATA_MONGODB_HOST_PROPERTY + ":localhost}") + private String host; + + @Value("${" + ServiceConstants.SECURITY_DATA_MONGODB_PORT_PROPERTY + ":27017}") + private int port; + + + public String getHost() { + return host; + } + + public int getPort() { + return port; + } +} diff --git a/core-service-lib/src/test/java/org/x2b/studi/core/TestGraphQLServiceConfigure.java b/core-service-lib/src/test/java/org/x2b/studi/core/TestGraphQLServiceConfigure.java index b5099c3..748e4ef 100644 --- a/core-service-lib/src/test/java/org/x2b/studi/core/TestGraphQLServiceConfigure.java +++ b/core-service-lib/src/test/java/org/x2b/studi/core/TestGraphQLServiceConfigure.java @@ -21,14 +21,7 @@ public String getValue() { return "this class is for testing schema creation but does not contain tests"; } } - - @Override - protected MongoClientFactoryBean createAuthDatasourceFactory() { - MongoClientFactoryBean factoryBean = new MongoClientFactoryBean(); - factoryBean.setHost("localhost"); - factoryBean.setPort(27017); - return factoryBean; - } + @Override protected RuntimeWiring createRuntimeWiring() { From 6f080aaea47c6c0ecb5a132ee0b3be7a099b8a03 Mon Sep 17 00:00:00 2001 From: Ethan Hunter Date: Sat, 21 Oct 2017 22:23:17 -0600 Subject: [PATCH 47/47] add standin secret for testing --- core-service-lib/src/main/resources/application.yaml | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/core-service-lib/src/main/resources/application.yaml b/core-service-lib/src/main/resources/application.yaml index f533c5e..dd86454 100644 --- a/core-service-lib/src/main/resources/application.yaml +++ b/core-service-lib/src/main/resources/application.yaml @@ -13,4 +13,9 @@ graphql: graphiql: mapping: /graphiql endpoint: /graphql - enabled: true \ No newline at end of file + enabled: true + +studi: + security: + auth: + secret: helasecret \ No newline at end of file