Skip to content

Commit

Permalink
#1 - The old vars user auth is snake case. Changed Oni's auth to snak…
Browse files Browse the repository at this point in the history
…e case
  • Loading branch information
hohonuuli committed Jul 2, 2024
1 parent 4a632dc commit 9bf1a6f
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 49 deletions.
54 changes: 27 additions & 27 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -81,33 +81,33 @@ lazy val oni = project
)
),
libraryDependencies ++= Seq(
auth0,
circeCore,
circeGeneric,
circeParser,
commonsCodec,
helidonEncodingDeflate,
helidonEncodingGzip,
hibernateCore,
hibernateEnvers,
hibernateHikari,
hikariCp,
jansi % Runtime,
jaspyt,
junit % Test,
logback,
mssqlserver,
munit % Test,
oracle,
postgresql,
slf4jSystem,
tapirCirce,
tapirHelidon,
tapirPrometheus,
tapirServerStub % Test,
tapirSttpCirce,
tapirSwagger,
typesafeConfig
auth0,
circeCore,
circeGeneric,
circeParser,
commonsCodec,
helidonEncodingDeflate,
helidonEncodingGzip,
hibernateCore,
hibernateEnvers,
hibernateHikari,
hikariCp,
jansi % Runtime,
jaspyt,
junit % Test,
logback,
mssqlserver,
munit % Test,
oracle,
postgresql,
slf4jSystem,
tapirCirce,
tapirHelidon,
tapirPrometheus,
tapirServerStub % Test,
tapirSttpCirce,
tapirSwagger,
typesafeConfig
)
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
package org.mbari.oni.endpoints

import io.circe.parser.decode
import org.mbari.oni.domain.{Authorization, UserAccount, UserAccountRoles}
import org.mbari.oni.domain.{AuthorizationSC, UserAccount, UserAccountRoles}
import org.mbari.oni.etc.circe.CirceCodecs.given
import org.mbari.oni.etc.jwt.JwtService
import org.mbari.oni.jpa.DatabaseFunSuite
Expand Down Expand Up @@ -49,10 +49,10 @@ trait AuthorizationEndpointsSuite extends DatabaseFunSuite with EndpointsSuite:
assert(response.body.isRight)

// println(body)
val d = decode[Authorization](body)
val d = decode[AuthorizationSC](body)
assert(d.isRight)
val bearerAuth = d.getOrElse(throw new Exception("No bearer auth"))
assert(jwtService.verify(bearerAuth.accessToken))
assert(jwtService.verify(bearerAuth.access_token))

test("login"):
val userService = UserAccountService(entityManagerFactory)
Expand Down Expand Up @@ -81,7 +81,7 @@ trait AuthorizationEndpointsSuite extends DatabaseFunSuite with EndpointsSuite:
assert(response.body.isRight)

// println(body)
val d = decode[Authorization](body)
val d = decode[AuthorizationSC](body)
assert(d.isRight)
val bearerAuth = d.getOrElse(throw new Exception("No bearer auth"))
assert(jwtService.verify(bearerAuth.accessToken))
assert(jwtService.verify(bearerAuth.access_token))
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@

package org.mbari.oni.domain

final case class Authorization(tokenType: String, accessToken: String)
final case class AuthorizationSC(token_type: String, access_token: String)

object Authorization:
object AuthorizationSC:
val TokenTypeBearer: String = "Bearer"
val TokenTypeApiKey: String = "APIKey"

def bearer(accessToken: String): Authorization = Authorization(TokenTypeBearer, accessToken)
def bearer(accessToken: String): AuthorizationSC = AuthorizationSC(TokenTypeBearer, accessToken)
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import sttp.tapir.Endpoint
import sttp.tapir.generic.auto.*
import sttp.tapir.json.circe.*
import sttp.tapir.server.ServerEndpoint
import org.mbari.oni.domain.{Authorization, BadRequest, ErrorMsg, NotFound, ServerError, Unauthorized}
import org.mbari.oni.domain.{AuthorizationSC, BadRequest, ErrorMsg, NotFound, ServerError, Unauthorized}
import org.mbari.oni.etc.circe.CirceCodecs.given
import org.mbari.oni.services.UserAccountService
import sttp.tapir.model.UsernamePassword
Expand All @@ -29,7 +29,7 @@ class AuthorizationEndpoints(entityManagerFactory: EntityManagerFactory)(using j
private val base = "auth"
private val tag = "Authorization"

val authEndpoint: Endpoint[String, Unit, ErrorMsg, Authorization, Any] =
val authEndpoint: Endpoint[String, Unit, ErrorMsg, AuthorizationSC, Any] =
baseEndpoint
.post
.in(base)
Expand All @@ -38,7 +38,7 @@ class AuthorizationEndpoints(entityManagerFactory: EntityManagerFactory)(using j
"Header format is `Authorization: APIKEY <key>`"
)
)
.out(jsonBody[Authorization])
.out(jsonBody[AuthorizationSC])
.errorOut(
oneOf[ErrorMsg](
oneOfVariant(statusCode(StatusCode.BadRequest).and(jsonBody[BadRequest])),
Expand All @@ -65,7 +65,7 @@ class AuthorizationEndpoints(entityManagerFactory: EntityManagerFactory)(using j
val apiKey = parts(1)
jwtService.authorize(apiKey) match
case None => Left(Unauthorized("Invalid API key"))
case Some(jwt) => Right(Authorization.bearer(jwt))
case Some(jwt) => Right(AuthorizationSC.bearer(jwt))
)
.serverLogic(bearerAuth => Unit => Right(bearerAuth))

Expand All @@ -74,7 +74,7 @@ class AuthorizationEndpoints(entityManagerFactory: EntityManagerFactory)(using j
.post
.in(base / "login")
.securityIn(auth.basic[UsernamePassword](WWWAuthenticateChallenge.basic(AuthenticationScheme.Basic.name)))
.out(jsonBody[Authorization])
.out(jsonBody[AuthorizationSC])
.errorOut(
oneOf[ErrorMsg](
oneOfVariant(statusCode(StatusCode.BadRequest).and(jsonBody[BadRequest])),
Expand Down Expand Up @@ -103,7 +103,7 @@ class AuthorizationEndpoints(entityManagerFactory: EntityManagerFactory)(using j
jwt <- jwtService
.login(usernamePassword.username, usernamePassword.password.getOrElse(""), entity)
.toRight(Unauthorized("Invalid username or password"))
yield Authorization.bearer(jwt)
yield AuthorizationSC.bearer(jwt)
}
.serverLogic(bearerAuth => Unit => Right(bearerAuth))

Expand Down
4 changes: 2 additions & 2 deletions oni/src/main/scala/org/mbari/oni/etc/circe/CirceCodecs.scala
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ object CirceCodecs:
given Encoder[Unauthorized] = deriveEncoder

// --- Domain Objects ---
given Decoder[Authorization] = deriveDecoder
given Encoder[Authorization] = deriveEncoder
given Decoder[AuthorizationSC] = deriveDecoder
given Encoder[AuthorizationSC] = deriveEncoder

given Decoder[Concept] = deriveDecoder
given Encoder[Concept] = deriveEncoder
Expand Down
13 changes: 7 additions & 6 deletions project/Dependencies.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@ object Dependencies {
lazy val commonsCodec = "commons-codec" % "commons-codec" % "1.17.0"
lazy val gson = "com.google.code.gson" % "gson" % "2.11.0"

// THis needs to match the version used by tapirHelidon
// THis needs to match the version used by tapirHelidon.
// Just including these in the build allows Helidon to use them for content encoding.
val helidonVersion = "4.0.0"
lazy val helidonEncodingDeflate = "io.helidon.http.encoding" % "helidon-http-encoding-deflate" % helidonVersion
lazy val helidonEncodingGzip = "io.helidon.http.encoding" % "helidon-http-encoding-gzip" % helidonVersion
lazy val helidonEncodingGzip = "io.helidon.http.encoding" % "helidon-http-encoding-gzip" % helidonVersion

val hibernateVersion = "6.5.2.Final"
lazy val hibernateCore = "org.hibernate.orm" % "hibernate-core" % hibernateVersion
Expand All @@ -30,7 +31,7 @@ object Dependencies {
lazy val munit = "org.scalameta" %% "munit" % "1.0.0"
lazy val oracle = "com.oracle.ojdbc" % "ojdbc8" % "19.3.0.0"
lazy val postgresql = "org.postgresql" % "postgresql" % "42.7.3"
lazy val scilube = "org.mbari.scilube" %% "scilube" % "3.0.1"
// lazy val scilube = "org.mbari.scilube" %% "scilube" % "3.0.1"

val slf4jVersion = "2.0.13"
lazy val slf4jJulBridge = "org.slf4j" % "jul-to-slf4j" % slf4jVersion
Expand All @@ -54,8 +55,8 @@ object Dependencies {
lazy val testcontainersPostgres = "org.testcontainers" % "postgresql" % testcontainersVersion

lazy val typesafeConfig = "com.typesafe" % "config" % "1.4.3"
lazy val uuidgen = "org.mbari.uuid" % "uuid-gen" % "0.1.4"
lazy val vcr4jCore = "org.mbari.vcr4j" % "vcr4j-core" % "5.2.0"
lazy val zeromq = "org.zeromq" % "jeromq" % "0.6.0"
// lazy val uuidgen = "org.mbari.uuid" % "uuid-gen" % "0.1.4"
// lazy val vcr4jCore = "org.mbari.vcr4j" % "vcr4j-core" % "5.2.0"
// lazy val zeromq = "org.zeromq" % "jeromq" % "0.6.0"

}

0 comments on commit 9bf1a6f

Please sign in to comment.