This repository has been archived by the owner on May 7, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from javaBin/bruker-registrering
Merge bruker-registrering
- Loading branch information
Showing
34 changed files
with
532 additions
and
242 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
4 changes: 2 additions & 2 deletions
4
backend/app/src/main/kotlin/backend/ViewRegisteredWorkshops.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
7 changes: 0 additions & 7 deletions
7
backend/app/src/main/kotlin/backend/admin/AdminWorkshopRegistration.kt
This file was deleted.
Oops, something went wrong.
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,51 @@ | ||
package backend.config | ||
|
||
import backend.repository.UserRepository | ||
import com.auth0.jwk.JwkProviderBuilder | ||
import com.auth0.jwt.interfaces.Payload | ||
import io.ktor.server.application.* | ||
import io.ktor.server.auth.* | ||
import io.ktor.server.auth.jwt.* | ||
import java.util.concurrent.TimeUnit | ||
|
||
class CustomPrincipal(payload: Payload, val userId: Int): Principal, JWTPayloadHolder(payload) | ||
|
||
fun Application.configureAuth() { | ||
fun validateCreds(credential: JWTCredential): CustomPrincipal? { | ||
val containsAudience = credential.payload.audience.contains(environment.config.property("auth0.audience").getString()) | ||
|
||
if (containsAudience) { | ||
val userRepository = UserRepository() | ||
|
||
val subject = credential.payload.subject | ||
val providerId = subject.split("|")[1] | ||
val provider = userRepository.findProviderById(providerId) ?: throw Exception("Provider not found") | ||
return CustomPrincipal(credential.payload, provider.userId) | ||
} | ||
|
||
return null | ||
} | ||
|
||
val issuer = environment.config.property("auth0.issuer").getString() | ||
val jwkProvider = JwkProviderBuilder(issuer) | ||
.cached(10, 24, TimeUnit.HOURS) | ||
.rateLimited(10, 1, TimeUnit.MINUTES) | ||
.build() | ||
|
||
install(Authentication) { | ||
jwt("auth0") { | ||
verifier(jwkProvider, issuer) | ||
validate { credential -> validateCreds(credential) } | ||
} | ||
basic("basic-auth0") { | ||
realm = "Used by auth0 for creating users" | ||
validate { credentials -> | ||
if (credentials.name == "auth0" && credentials.password == "password") { | ||
UserIdPrincipal(credentials.name) | ||
} else { | ||
null | ||
} | ||
} | ||
} | ||
} | ||
} |
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,33 @@ | ||
package backend.config | ||
|
||
import io.ktor.client.* | ||
import io.ktor.client.plugins.contentnegotiation.* | ||
import io.ktor.client.plugins.logging.* | ||
import io.ktor.http.* | ||
import io.ktor.http.HttpHeaders.ContentEncoding | ||
import io.ktor.serialization.kotlinx.json.* | ||
import io.netty.handler.codec.compression.StandardCompressionOptions.deflate | ||
import io.netty.handler.codec.compression.StandardCompressionOptions.gzip | ||
import kotlinx.serialization.json.Json | ||
|
||
fun HttpClientConfig<*>.defaultClient() { | ||
install(ContentNegotiation) { | ||
json(Json { | ||
prettyPrint = true | ||
isLenient = true | ||
ignoreUnknownKeys = true | ||
}) | ||
} | ||
|
||
install(ContentEncoding){ | ||
gzip() | ||
deflate() | ||
} | ||
|
||
install(Logging) { | ||
logger = Logger.DEFAULT | ||
level = LogLevel.ALL | ||
sanitizeHeader { header -> header == HttpHeaders.Authorization } | ||
} | ||
// Add all the common configuration here. | ||
} |
66 changes: 66 additions & 0 deletions
66
backend/app/src/main/kotlin/backend/config/DatabaseFactory.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,66 @@ | ||
package com.inventy.plugins | ||
|
||
import com.zaxxer.hikari.HikariDataSource | ||
import java.util.concurrent.TimeUnit.MINUTES | ||
import org.jetbrains.exposed.sql.* | ||
import kotlinx.coroutines.* | ||
import org.flywaydb.core.Flyway | ||
import org.postgresql.ds.PGSimpleDataSource | ||
import org.h2.jdbcx.JdbcDataSource | ||
import org.jetbrains.exposed.sql.transactions.experimental.newSuspendedTransaction | ||
|
||
class DatabaseFactory( | ||
private val dbHost: String, | ||
private val dbPort: String, | ||
private val dbUser: String, | ||
private val dbPassword: String, | ||
private val databaseName: String, | ||
private val embedded: Boolean = false | ||
) { | ||
|
||
companion object { | ||
suspend fun <T> dbQuery(block: suspend () -> T): T = | ||
newSuspendedTransaction(Dispatchers.IO) { block() } | ||
} | ||
|
||
fun init() { | ||
Database.connect(hikari()) | ||
val flyway = Flyway.configure() | ||
.dataSource(hikari()) | ||
.load() | ||
flyway.migrate() | ||
} | ||
|
||
private fun hikari(): HikariDataSource { | ||
if (embedded) { | ||
return HikariDataSource().apply { | ||
dataSourceClassName = JdbcDataSource::class.qualifiedName | ||
addDataSourceProperty("url", "jdbc:h2:mem:inventy;DB_CLOSE_DELAY=-1") | ||
addDataSourceProperty("user", "root") | ||
addDataSourceProperty("password", "") | ||
maximumPoolSize = 10 | ||
minimumIdle = 1 | ||
idleTimeout = 100000 | ||
connectionTimeout = 100000 | ||
maxLifetime = MINUTES.toMillis(30) | ||
} | ||
} else { | ||
return HikariDataSource().apply { | ||
dataSourceClassName = PGSimpleDataSource::class.qualifiedName | ||
addDataSourceProperty("serverName", dbHost) | ||
addDataSourceProperty("portNumber", dbPort) | ||
addDataSourceProperty("user", dbUser) | ||
addDataSourceProperty("password", dbPassword) | ||
addDataSourceProperty("databaseName", databaseName) | ||
maximumPoolSize = 10 | ||
minimumIdle = 1 | ||
idleTimeout = 100000 | ||
connectionTimeout = 100000 | ||
maxLifetime = MINUTES.toMillis(30) | ||
} | ||
} | ||
|
||
|
||
} | ||
|
||
} |
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,43 @@ | ||
package backend.config | ||
|
||
import backend.model.* | ||
|
||
|
||
class TestData { | ||
companion object { | ||
val provider1 = listOf(Provider(1, 1, ProviderType.GOOGLE.id, "1234567890")) | ||
|
||
val user1 = User(1, "John", "Doe", "[email protected]", "", provider1) | ||
val user2 = User(2, "Jane", "Doe", "[email protected]","", provider1) | ||
val user3 = User(3, "John", "Smith", "[email protected]","", provider1) | ||
|
||
val workshop1 = Workshop(1, "Kotlin", "John Doe") | ||
val workshop2 = Workshop(2, "Ktor", "Jane Doe") | ||
val workshop3 = Workshop(3, "Kotlin Multiplatform", "John Doe") | ||
|
||
|
||
val userMap = | ||
mutableMapOf( | ||
1 to user1, | ||
2 to user2, | ||
3 to user3, | ||
) | ||
|
||
val registrationMap = | ||
mutableMapOf( | ||
1 to WorkshopRegistration(1, user1, workshop1, WorkshopRegistrationState.APPROVED), | ||
2 to WorkshopRegistration(2, user1, workshop2, WorkshopRegistrationState.APPROVED), | ||
3 to WorkshopRegistration(3, user2, workshop1, WorkshopRegistrationState.APPROVED), | ||
4 to WorkshopRegistration(4, user3, workshop3, WorkshopRegistrationState.APPROVED), | ||
) | ||
|
||
val workshopMap: MutableMap<Int, Workshop> = | ||
mutableMapOf( | ||
1 to workshop1, | ||
2 to workshop2, | ||
3 to workshop3, | ||
) | ||
|
||
} | ||
} | ||
|
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
5 changes: 3 additions & 2 deletions
5
.../kotlin/backend/admin/AdminWorkshopDTO.kt → ...in/kotlin/backend/dto/AdminWorkshopDTO.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,10 +1,11 @@ | ||
package backend.admin | ||
package backend.dto | ||
|
||
import backend.dto.AdminWorkshopRegistrationDTO | ||
import kotlinx.serialization.Serializable | ||
|
||
@Serializable | ||
data class AdminWorkshopDTO( | ||
val title: String, | ||
val teacherName: String, | ||
val registrations: List<AdminWorkshopRegistration>, | ||
val registrations: List<AdminWorkshopRegistrationDTO>, | ||
) |
Oops, something went wrong.