Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
82 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
src/main/kotlin/de/code_freak/codefreak/graphql/AuthApi.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package de.code_freak.codefreak.graphql | ||
|
||
import com.expediagroup.graphql.annotations.GraphQLName | ||
import com.expediagroup.graphql.spring.operations.Mutation | ||
import de.code_freak.codefreak.util.FrontendUtil | ||
import org.springframework.beans.factory.annotation.Autowired | ||
import org.springframework.security.authentication.AuthenticationManager | ||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken | ||
import org.springframework.security.core.context.SecurityContextHolder | ||
import org.springframework.security.web.context.HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY | ||
import org.springframework.stereotype.Component | ||
|
||
@GraphQLName("Authentication") | ||
class AuthenticationDto(val token: String, val user: UserDto) | ||
|
||
@Component | ||
class AuthMutation : Mutation { | ||
|
||
@Autowired(required = false) | ||
private lateinit var authenticationManager: AuthenticationManager | ||
|
||
fun login(username: String, password: String): AuthenticationDto { | ||
val auth = authenticationManager.authenticate(UsernamePasswordAuthenticationToken(username, password)) | ||
val securityContext = SecurityContextHolder.getContext() | ||
securityContext.authentication = auth | ||
val session = FrontendUtil.getRequest().getSession(true) | ||
session.setAttribute(SPRING_SECURITY_CONTEXT_KEY, securityContext) | ||
|
||
return AuthenticationDto(session.id, UserDto(FrontendUtil.getCurrentUser().entity)) | ||
} | ||
} |
17 changes: 1 addition & 16 deletions
17
src/main/kotlin/de/code_freak/codefreak/graphql/CustomSchemaGeneratorHooksImpl.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,13 @@ | ||
package de.code_freak.codefreak.graphql | ||
|
||
import com.expediagroup.graphql.hooks.SchemaGeneratorHooks | ||
import graphql.schema.GraphQLScalarType | ||
import graphql.schema.GraphQLType | ||
import org.springframework.stereotype.Component | ||
import java.time.Instant | ||
import java.util.UUID | ||
import kotlin.reflect.KType | ||
|
||
@Component | ||
class CustomSchemaGeneratorHooksImpl : SchemaGeneratorHooks { | ||
override fun willGenerateGraphQLType(type: KType): GraphQLType? { | ||
return when (type.classifier) { | ||
UUID::class -> GraphQLScalarType.Builder() | ||
.name("ID") | ||
.description("ID to java.util.UUID") | ||
.coercing(UuidConverter()) | ||
.build() | ||
Instant::class -> GraphQLScalarType.Builder() | ||
.name("DateTime") | ||
.description("DateTime to java.time.Instant") | ||
.coercing(DateTimeConverter()) | ||
.build() | ||
else -> null | ||
} | ||
return type.classifier?.let { ScalarTypes.get(it) } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
src/main/kotlin/de/code_freak/codefreak/graphql/ScalarTypes.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package de.code_freak.codefreak.graphql | ||
|
||
import graphql.schema.GraphQLScalarType | ||
import java.time.Instant | ||
import java.util.UUID | ||
import kotlin.reflect.KClassifier | ||
|
||
object ScalarTypes { | ||
private val scalars = mapOf( | ||
UUID::class to GraphQLScalarType.Builder() | ||
.name("ID") | ||
.description("ID to java.util.UUID") | ||
.coercing(UuidConverter()) | ||
.build(), | ||
Instant::class to GraphQLScalarType.Builder() | ||
.name("DateTime") | ||
.description("DateTime to java.time.Instant") | ||
.coercing(DateTimeConverter()) | ||
.build() | ||
) | ||
fun get(classifier: KClassifier) = scalars[classifier] | ||
} |
11 changes: 11 additions & 0 deletions
11
src/main/kotlin/de/code_freak/codefreak/graphql/UserApi.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package de.code_freak.codefreak.graphql | ||
|
||
import com.expediagroup.graphql.annotations.GraphQLID | ||
import com.expediagroup.graphql.annotations.GraphQLName | ||
import de.code_freak.codefreak.entity.User | ||
import java.util.UUID | ||
|
||
@GraphQLName("User") | ||
class UserDto(@GraphQLID val id: UUID, val username: String) { | ||
constructor(user: User) : this(user.id, user.username) | ||
} |
This file was deleted.
Oops, something went wrong.