Skip to content

Commit

Permalink
Merge pull request #245 from RADAR-base/release-4.4.0
Browse files Browse the repository at this point in the history
Release 4.4.0
  • Loading branch information
mpgxvii authored Oct 30, 2023
2 parents dae59b2 + 7ed4f5e commit 6867f5d
Show file tree
Hide file tree
Showing 52 changed files with 999 additions and 653 deletions.
4 changes: 0 additions & 4 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,8 @@ charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.kt]
ktlint_standard_no-wildcard-imports=disabled

[*.md]
trim_trailing_whitespace = false

[*.{json,yaml,yml}]
indent_style = space
indent_size = 2
4 changes: 3 additions & 1 deletion authorizer-app-backend/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,17 @@
# See the License for the specific language governing permissions and
# limitations under the License.

FROM --platform=$BUILDPLATFORM gradle:7.6-jdk17 as builder
FROM --platform=$BUILDPLATFORM gradle:8.3-jdk17 as builder

RUN mkdir /code
WORKDIR /code

ENV GRADLE_USER_HOME=/code/.gradlecache \
GRADLE_OPTS="-Djdk.lang.Process.launchMechanism=vfork -Dorg.gradle.vfs.watch=false"

COPY ./buildSrc /code/buildSrc
COPY ./build.gradle.kts ./settings.gradle.kts ./gradle.properties /code/
COPY ./buildSrc /code/buildSrc
COPY authorizer-app-backend/build.gradle.kts /code/authorizer-app-backend/
RUN gradle downloadDependencies copyDependencies startScripts

Expand Down
87 changes: 21 additions & 66 deletions authorizer-app-backend/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,86 +1,41 @@
import org.gradle.api.tasks.testing.logging.TestExceptionFormat.FULL
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
application
kotlin("jvm")
id("org.jetbrains.kotlin.plugin.noarg")
id("org.jetbrains.kotlin.plugin.jpa")
id("org.jetbrains.kotlin.plugin.allopen")
kotlin("plugin.serialization")
kotlin("plugin.noarg")
kotlin("plugin.jpa")
kotlin("plugin.allopen")
}

application {
mainClass.set("org.radarbase.authorizer.Main")
applicationDefaultJvmArgs = listOf(
"-Djava.security.egd=file:/dev/./urandom",
"-Djava.util.logging.manager=org.apache.logging.log4j.jul.LogManager",
)
}

repositories {
maven(url = "https://oss.sonatype.org/content/repositories/snapshots")
}

dependencies {
api(kotlin("stdlib-jdk8"))
implementation(kotlin("reflect"))

val radarJerseyVersion: String by project
implementation("org.radarbase:radar-jersey:$radarJerseyVersion")
implementation("org.radarbase:radar-jersey-hibernate:$radarJerseyVersion") {
val postgresVersion: String by project
runtimeOnly("org.postgresql:postgresql:$postgresVersion")
implementation("org.radarbase:radar-jersey:${Versions.radarJersey}")
implementation("org.radarbase:radar-jersey-hibernate:${Versions.radarJersey}") {
runtimeOnly("org.postgresql:postgresql:${Versions.postgresql}")
}
implementation("org.radarbase:radar-commons-kotlin:${Versions.radarCommons}")

val slf4jVersion: String by project
implementation("org.slf4j:slf4j-api:$slf4jVersion")

val okhttpVersion: String by project
implementation("com.squareup.okhttp3:okhttp:$okhttpVersion")
implementation("redis.clients:jedis:${Versions.jedis}")

val jedisVersion: String by project
implementation("redis.clients:jedis:$jedisVersion")
implementation(enforcedPlatform("io.ktor:ktor-bom:${Versions.ktor}"))
implementation("io.ktor:ktor-client-core")
implementation("io.ktor:ktor-client-auth")
implementation("io.ktor:ktor-client-cio")
implementation("io.ktor:ktor-client-content-negotiation")
implementation("io.ktor:ktor-serialization-kotlinx-json")

val log4j2Version: String by project
runtimeOnly("org.apache.logging.log4j:log4j-core:$log4j2Version")
runtimeOnly("org.apache.logging.log4j:log4j-slf4j2-impl:$log4j2Version")
runtimeOnly("org.apache.logging.log4j:log4j-jul:$log4j2Version")

val junitVersion: String by project
testImplementation("org.junit.jupiter:junit-jupiter:$junitVersion")
testImplementation("org.hamcrest:hamcrest-all:1.3")

val mockitoKotlinVersion: String by project
testImplementation("org.mockito.kotlin:mockito-kotlin:$mockitoKotlinVersion")

val jerseyVersion: String by project
testImplementation("org.glassfish.jersey.test-framework.providers:jersey-test-framework-provider-grizzly2:$jerseyVersion")
}

tasks.withType<KotlinCompile> {
kotlinOptions {
jvmTarget = "17"
apiVersion = "1.7"
languageVersion = "1.7"
}
}

tasks.withType<JavaCompile> {
options.release.set(17)
}

tasks.withType<Test> {
useJUnitPlatform()
testLogging {
events("passed", "skipped", "failed")
showStandardStreams = true
exceptionFormat = FULL
}
systemProperty("java.util.logging.manager", "org.apache.logging.log4j.jul.LogManager")
testImplementation("org.hamcrest:hamcrest:${Versions.hamcrest}")
testImplementation("org.mockito.kotlin:mockito-kotlin:${Versions.mockitoKotlin}")
testImplementation("org.glassfish.jersey.test-framework.providers:jersey-test-framework-provider-grizzly2:${Versions.jersey}")
}

allOpen {
annotation("javax.persistence.Entity")
annotation("javax.persistence.MappedSuperclass")
annotation("javax.persistence.Embeddable")
annotation("jakarta.persistence.Entity")
annotation("jakarta.persistence.MappedSuperclass")
annotation("jakarta.persistence.Embeddable")
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,27 +16,48 @@

package org.radarbase.authorizer.api

import com.fasterxml.jackson.annotation.JsonIgnoreProperties
import com.fasterxml.jackson.annotation.JsonProperty
import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable
import java.time.Instant

@JsonIgnoreProperties(ignoreUnknown = true)
@Serializable
data class RestOauth2AccessToken(
@JsonProperty("access_token") val accessToken: String,
@JsonProperty("refresh_token") val refreshToken: String? = null,
@JsonProperty("expires_in") val expiresIn: Int = 0,
@JsonProperty("token_type") val tokenType: String? = null,
@JsonProperty("user_id") val externalUserId: String? = null,
@SerialName("access_token")
val accessToken: String,
@SerialName("refresh_token")
val refreshToken: String? = null,
@SerialName("expires_in")
val expiresIn: Int = 0,
@SerialName("token_type")
val tokenType: String? = null,
@SerialName("user_id")
val externalUserId: String? = null,
)

@Serializable
data class RestOauth1AccessToken(
@JsonProperty("oauth_token") val token: String,
@JsonProperty("oauth_token_secret") val tokenSecret: String? = null,
@JsonProperty("oauth_verifier") val tokenVerifier: String? = null,
@SerialName("oauth_token")
val token: String,
@SerialName("oauth_token_secret")
val tokenSecret: String? = null,
@SerialName("oauth_verifier")
val tokenVerifier: String? = null,
)

@Serializable
data class RestOauth1UserId(
@JsonProperty("userId") val userId: String,
@SerialName("userId")
val userId: String,
)

@Serializable
data class OuraAuthUserId(
val age: Int,
val weight: Float,
val height: Float,
val gender: String,
val email: String,
@SerialName("user_id") val userId: String,
)

data class SignRequestParams(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ package org.radarbase.authorizer.api
import jakarta.ws.rs.core.Context
import org.radarbase.authorizer.doa.entity.RestSourceUser
import org.radarbase.jersey.service.managementportal.RadarProjectService
import org.radarbase.kotlin.coroutines.forkJoin

class RestSourceUserMapper(
@Context private val projectService: RadarProjectService,
) {
fun fromEntity(user: RestSourceUser): RestSourceUserDTO {
suspend fun fromEntity(user: RestSourceUser): RestSourceUserDTO {
val mpUser = user.projectId?.let { p ->
user.userId?.let { u -> projectService.subject(p, u) }
}
Expand All @@ -48,8 +49,8 @@ class RestSourceUserMapper(
)
}

fun fromRestSourceUsers(records: List<RestSourceUser>, page: Page?) = RestSourceUsers(
users = records.map(::fromEntity),
suspend fun fromRestSourceUsers(records: List<RestSourceUser>, page: Page?) = RestSourceUsers(
users = records.forkJoin { fromEntity(it) },
metadata = page,
)
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package org.radarbase.authorizer.config

import okhttp3.HttpUrl
import okhttp3.HttpUrl.Companion.toHttpUrlOrNull
import io.ktor.http.URLBuilder
import io.ktor.http.Url
import io.ktor.http.appendPathSegments
import io.ktor.http.takeFrom
import org.radarbase.authorizer.enhancer.ManagementPortalEnhancerFactory
import org.radarbase.jersey.enhancer.EnhancerFactory
import java.net.URI
Expand All @@ -18,26 +20,20 @@ data class AuthorizerServiceConfig(
val tokenExpiryTimeInMinutes: Long = 15,
val persistentTokenExpiryInMin: Long = 3.days.inWholeMinutes,
) {
val callbackUrl: HttpUrl by lazy {
val callbackUrl: Url by lazy {
val frontendBaseUrlBuilder = when {
frontendBaseUri != null -> frontendBaseUri.toHttpUrlOrNull()?.newBuilder()
advertisedBaseUri != null -> {
advertisedBaseUri.toHttpUrlOrNull()?.let { advertisedUrl ->
advertisedUrl.newBuilder().apply {
advertisedUrl.pathSegments.asReversed()
.forEachIndexed { idx, segment ->
if (segment.isEmpty() || segment == "backend") {
removePathSegment(advertisedUrl.pathSize - 1 - idx)
}
}
addPathSegment("authorizer")
}
frontendBaseUri != null -> URLBuilder().takeFrom(frontendBaseUri)
advertisedBaseUri != null -> URLBuilder().apply {
takeFrom(advertisedBaseUri)
pathSegments = buildList(pathSegments.size) {
addAll(pathSegments.dropLastWhile { it.isEmpty() || it == "backend" })
add("authorizer")
}
}
else -> null
else -> throw IllegalStateException("Frontend URL parameter is not a valid HTTP URL.")
}
checkNotNull(frontendBaseUrlBuilder) { "Frontend URL parameter $frontendBaseUri is not a valid HTTP URL." }
.addPathSegment("users:new")
frontendBaseUrlBuilder
.appendPathSegments("users:new")
.build()
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package org.radarbase.authorizer.config

import org.radarbase.jersey.config.ConfigLoader.copyEnv
import java.util.*
import java.util.Locale

data class RestSourceClient(
val sourceType: String,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,23 @@ import org.radarbase.authorizer.config.AuthorizerConfig
import org.radarbase.authorizer.doa.entity.RegistrationState
import org.radarbase.authorizer.doa.entity.RestSourceUser
import org.radarbase.authorizer.util.Hmac256Secret
import org.radarbase.authorizer.util.Hmac256Secret.Companion.encodeToBase64
import org.radarbase.authorizer.util.Hmac256Secret.Companion.randomize
import org.radarbase.authorizer.util.encodeToBase64
import org.radarbase.authorizer.util.randomize
import org.radarbase.jersey.hibernate.HibernateRepository
import org.radarbase.jersey.service.AsyncCoroutineService
import java.time.Instant
import kotlin.time.Duration.Companion.minutes

class RegistrationRepository(
@Context private val config: AuthorizerConfig,
@Context em: Provider<EntityManager>,
) : HibernateRepository(em) {
@Context asyncService: AsyncCoroutineService,
) : HibernateRepository(em, asyncService) {

private val tokenExpiryTime = config.service.tokenExpiryTimeInMinutes.minutes
private val persistentTokenExpiryTime = config.service.persistentTokenExpiryInMin.minutes

fun generate(
suspend fun generate(
user: RestSourceUser,
secret: Hmac256Secret?,
persistent: Boolean,
Expand Down Expand Up @@ -53,11 +55,11 @@ class RegistrationRepository(
}
}

operator fun get(token: String): RegistrationState? = transact {
suspend fun get(token: String): RegistrationState? = transact {
find(RegistrationState::class.java, token)
}

fun cleanUp(): Int = transact {
suspend fun cleanUp(): Int = transact {
val cb = criteriaBuilder

// create delete
Expand All @@ -70,15 +72,11 @@ class RegistrationRepository(
createQuery(deleteQuery).executeUpdate()
}

operator fun minusAssign(token: String) = remove(token)

operator fun minusAssign(registrationState: RegistrationState) = remove(registrationState)

fun remove(registrationState: RegistrationState): Unit = transact {
suspend fun remove(registrationState: RegistrationState): Unit = transact {
remove(registrationState)
}

fun remove(token: String): Unit = transact {
suspend fun remove(token: String): Unit = transact {
val state = find(RegistrationState::class.java, token)
remove(state)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,20 @@ import org.radarbase.authorizer.doa.entity.RestSourceUser
import java.time.Instant

interface RestSourceUserRepository {
fun create(user: RestSourceUserDTO): RestSourceUser
fun updateToken(token: RestOauth2AccessToken?, user: RestSourceUser): RestSourceUser
fun read(id: Long): RestSourceUser?
fun update(userId: Long, user: RestSourceUserDTO): RestSourceUser
fun query(
suspend fun create(user: RestSourceUserDTO): RestSourceUser
suspend fun updateToken(token: RestOauth2AccessToken?, user: RestSourceUser): RestSourceUser
suspend fun read(id: Long): RestSourceUser?
suspend fun update(userId: Long, user: RestSourceUserDTO): RestSourceUser
suspend fun query(
page: Page,
projectIds: List<String>,
sourceType: String? = null,
search: String?,
userIds: List<String>,
isAuthorized: Boolean?,
): Pair<List<RestSourceUser>, Page>
fun queryAllWithElapsedEndDate(sourceType: String? = null): List<RestSourceUser>
fun delete(user: RestSourceUser)
fun reset(user: RestSourceUser, startDate: Instant, endDate: Instant?): RestSourceUser
fun findByExternalId(externalId: String, sourceType: String): RestSourceUser?
suspend fun queryAllWithElapsedEndDate(sourceType: String? = null): List<RestSourceUser>
suspend fun delete(user: RestSourceUser)
suspend fun reset(user: RestSourceUser, startDate: Instant, endDate: Instant?): RestSourceUser
suspend fun findByExternalId(externalId: String, sourceType: String): RestSourceUser?
}
Loading

0 comments on commit 6867f5d

Please sign in to comment.