Skip to content

Commit

Permalink
chore: minor cleanup
Browse files Browse the repository at this point in the history
Signed-off-by: Pat Losoponkul <pat.losoponkul@iohk.io>
  • Loading branch information
Pat Losoponkul committed Oct 11, 2023
1 parent 0ba019a commit 02df2ad
Show file tree
Hide file tree
Showing 17 changed files with 53 additions and 56 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,16 +46,16 @@ trait Authorizer[E <: BaseEntity] {
def authorize(entity: E): IO[AuthenticationError, WalletId]
}

trait AuthenticatorAuthorizer[E <: BaseEntity] extends Authenticator[E], Authorizer[E]

object EntityAuthorizer extends EntityAuthorizer

trait EntityAuthorizer extends Authorizer[Entity] {
override def authorize(entity: Entity): IO[AuthenticationError, WalletId] =
ZIO.succeed(entity.walletId).map(WalletId.fromUUID)
}

object DefaultEntityAuthenticator extends AuthenticatorAuthorizer[BaseEntity] {
trait AuthenticatorWithAuthZ[E <: BaseEntity] extends Authenticator[E], Authorizer[E]

object DefaultEntityAuthenticator extends AuthenticatorWithAuthZ[BaseEntity] {

override def isEnabled: Boolean = true
override def authenticate(credentials: Credentials): IO[AuthenticationError, BaseEntity] = ZIO.succeed(Entity.Default)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ case class DefaultAuthenticator(
adminApiKeyAuthenticator: AdminApiKeyAuthenticator,
apiKeyAuthenticator: ApiKeyAuthenticator,
keycloakAuthenticator: KeycloakAuthenticator
) extends Authenticator[BaseEntity],
Authorizer[BaseEntity] {
) extends AuthenticatorWithAuthZ[BaseEntity] {

override def isEnabled = true

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package io.iohk.atala.iam.authentication.admin

import io.iohk.atala.agent.walletapi.model.Entity
import io.iohk.atala.iam.authentication.{AuthenticationError, Authenticator, Credentials, EntityAuthorizer}
import io.iohk.atala.iam.authentication.AuthenticatorWithAuthZ
import io.iohk.atala.iam.authentication.EntityAuthorizer
import io.iohk.atala.iam.authentication.{AuthenticationError, Credentials}
import zio.IO

trait AdminApiKeyAuthenticator extends Authenticator[Entity], EntityAuthorizer {
trait AdminApiKeyAuthenticator extends AuthenticatorWithAuthZ[Entity], EntityAuthorizer {

def authenticate(credentials: Credentials): IO[AuthenticationError, Entity] = {
credentials match {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package io.iohk.atala.iam.authentication.apikey

import io.iohk.atala.agent.walletapi.model.Entity
import io.iohk.atala.iam.authentication.AuthenticationError.*
import io.iohk.atala.iam.authentication.{AuthenticationError, Authenticator, Credentials, EntityAuthorizer}
import io.iohk.atala.iam.authentication.AuthenticatorWithAuthZ
import io.iohk.atala.iam.authentication.EntityAuthorizer
import io.iohk.atala.iam.authentication.{AuthenticationError, Credentials}
import zio.{IO, ZIO}

import java.util.UUID

trait ApiKeyAuthenticator extends Authenticator[Entity], EntityAuthorizer {
trait ApiKeyAuthenticator extends AuthenticatorWithAuthZ[Entity], EntityAuthorizer {

def authenticate(credentials: Credentials): IO[AuthenticationError, Entity] = {
if (isEnabled) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
package io.iohk.atala.iam.authentication.oidc

import io.iohk.atala.agent.walletapi.model.BaseEntity
import io.iohk.atala.iam.authentication.AuthenticationError
import io.iohk.atala.iam.authentication.AuthenticationError.AuthenticationMethodNotEnabled
import io.iohk.atala.iam.authentication.AuthenticationError.InvalidCredentials
import io.iohk.atala.iam.authentication.Authenticator
import io.iohk.atala.iam.authentication.Authorizer
import io.iohk.atala.iam.authentication.AuthenticatorWithAuthZ
import io.iohk.atala.iam.authentication.Credentials
import zio.*

import java.util.UUID
import io.iohk.atala.agent.walletapi.model.BaseEntity

final case class KeycloakEntity(id: UUID, rawToken: String) extends BaseEntity

trait KeycloakAuthenticator extends Authenticator[KeycloakEntity], Authorizer[KeycloakEntity] {
trait KeycloakAuthenticator extends AuthenticatorWithAuthZ[KeycloakEntity] {
def authenticate(credentials: Credentials): IO[AuthenticationError, KeycloakEntity] = {
if (isEnabled) {
credentials match {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ class KeycloakAuthenticatorImpl(
}
}

// TODO: do not initialize when isEnable = false
object KeycloakAuthenticatorImpl {
val layer: RLayer[KeycloakClient & KeycloakConfig & WalletManagementService, KeycloakAuthenticator] =
ZLayer.fromFunction(KeycloakAuthenticatorImpl(_, _, _))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import io.iohk.atala.agent.walletapi.service.{ManagedDIDService, MockManagedDIDS
import io.iohk.atala.api.http.ErrorResponse
import io.iohk.atala.castor.core.service.MockDIDService
import io.iohk.atala.container.util.MigrationAspects.migrate
import io.iohk.atala.iam.authentication.AuthenticatorAuthorizer
import io.iohk.atala.iam.authentication.AuthenticatorWithAuthZ
import io.iohk.atala.issue.controller.http.AcceptCredentialOfferRequest
import sttp.client3.ziojson.*
import sttp.client3.{DeserializationException, UriContext, basicRequest}
Expand All @@ -26,7 +26,7 @@ object IssueControllerImplSpec extends ZIOSpecDefault with IssueControllerTestTo
test("provide incorrect recordId to endpoint") {
for {
issueControllerService <- ZIO.service[IssueController]
authenticator <- ZIO.service[AuthenticatorAuthorizer[BaseEntity]]
authenticator <- ZIO.service[AuthenticatorWithAuthZ[BaseEntity]]
backend = httpBackend(issueControllerService, authenticator)
response: IssueCredentialBadRequestResponse <- basicRequest
.post(uri"${issueUriBase}/records/12345/accept-offer")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import io.iohk.atala.api.http.ErrorResponse
import io.iohk.atala.castor.core.service.MockDIDService
import io.iohk.atala.connect.core.repository.ConnectionRepositoryInMemory
import io.iohk.atala.connect.core.service.ConnectionServiceImpl
import io.iohk.atala.iam.authentication.AuthenticatorAuthorizer
import io.iohk.atala.iam.authentication.AuthenticatorWithAuthZ
import io.iohk.atala.iam.authentication.DefaultEntityAuthenticator
import io.iohk.atala.iris.proto.service.IrisServiceGrpc
import io.iohk.atala.issue.controller.http.{
Expand Down Expand Up @@ -115,7 +115,7 @@ trait IssueControllerTestTools extends PostgresTestContainerSupport {
.defaultHandlers(ErrorResponse.failureResponseHandler)
}

def httpBackend(controller: IssueController, authenticator: AuthenticatorAuthorizer[BaseEntity]) = {
def httpBackend(controller: IssueController, authenticator: AuthenticatorWithAuthZ[BaseEntity]) = {
val issueEndpoints = IssueServerEndpoints(controller, authenticator)

val backend =
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,13 @@
package io.iohk.atala.pollux.credentialdefinition

import io.iohk.atala.agent.walletapi.model.BaseEntity
import io.iohk.atala.agent.walletapi.model.Entity
import io.iohk.atala.agent.walletapi.storage.GenericSecretStorage
import io.iohk.atala.api.http.ErrorResponse
import io.iohk.atala.container.util.MigrationAspects.*
import io.iohk.atala.iam.authentication.AuthenticatorWithAuthZ
import io.iohk.atala.pollux.core.model.secret.CredentialDefinitionSecret
import io.iohk.atala.pollux.core.service.serdes.{
PrivateCredentialDefinitionSchemaSerDesV1,
ProofKeyCredentialDefinitionSchemaSerDesV1,
PublicCredentialDefinitionSerDesV1
}
import io.iohk.atala.pollux.core.service.serdes.{ PrivateCredentialDefinitionSchemaSerDesV1, ProofKeyCredentialDefinitionSchemaSerDesV1, PublicCredentialDefinitionSerDesV1 }
import io.iohk.atala.pollux.credentialdefinition.controller.CredentialDefinitionController
import io.iohk.atala.pollux.credentialdefinition.http.{CredentialDefinitionInput, CredentialDefinitionResponse}
import sttp.client3.basicRequest
Expand All @@ -23,8 +21,6 @@ import zio.test.Assertion.*
import zio.test.TestAspect.*

import java.util.UUID
import io.iohk.atala.iam.authentication.AuthenticatorAuthorizer
import io.iohk.atala.agent.walletapi.model.BaseEntity

object CredentialDefinitionBasicSpec extends ZIOSpecDefault with CredentialDefinitionTestTools:

Expand Down Expand Up @@ -66,7 +62,7 @@ object CredentialDefinitionBasicSpec extends ZIOSpecDefault with CredentialDefin
val backendZIO =
for {
controller <- ZIO.service[CredentialDefinitionController]
authenticator <- ZIO.service[AuthenticatorAuthorizer[BaseEntity]]
authenticator <- ZIO.service[AuthenticatorWithAuthZ[BaseEntity]]
} yield httpBackend(controller, authenticator)

def createCredentialDefinitionResponseZIO = for {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import io.iohk.atala.agent.walletapi.model.BaseEntity
import io.iohk.atala.agent.walletapi.service.MockManagedDIDService
import io.iohk.atala.api.http.ErrorResponse
import io.iohk.atala.container.util.MigrationAspects.migrate
import io.iohk.atala.iam.authentication.AuthenticatorAuthorizer
import io.iohk.atala.iam.authentication.AuthenticatorWithAuthZ
import io.iohk.atala.pollux.credentialdefinition.controller.CredentialDefinitionController
import sttp.client3.ziojson.*
import sttp.client3.{DeserializationException, basicRequest}
Expand All @@ -27,7 +27,7 @@ object CredentialDefinitionFailureSpec extends ZIOSpecDefault with CredentialDef
test("create the credential definition with wrong json body returns BadRequest as json") {
for {
credentialDefinitionRegistryService <- ZIO.service[CredentialDefinitionController]
authenticator <- ZIO.service[AuthenticatorAuthorizer[BaseEntity]]
authenticator <- ZIO.service[AuthenticatorWithAuthZ[BaseEntity]]
backend = httpBackend(credentialDefinitionRegistryService, authenticator)
response: CredentialDefinitionBadRequestResponse <- basicRequest
.post(credentialDefinitionUriBase)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package io.iohk.atala.pollux.credentialdefinition

import io.iohk.atala.agent.walletapi.model.BaseEntity
import io.iohk.atala.container.util.MigrationAspects.migrate
import io.iohk.atala.iam.authentication.AuthenticatorAuthorizer
import io.iohk.atala.iam.authentication.AuthenticatorWithAuthZ
import io.iohk.atala.pollux.credentialdefinition.controller.CredentialDefinitionController
import io.iohk.atala.pollux.credentialdefinition.http.{CredentialDefinitionResponse, CredentialDefinitionResponsePage}
import io.iohk.atala.shared.models.{WalletAccessContext, WalletId}
Expand All @@ -22,12 +22,12 @@ object CredentialDefinitionLookupAndPaginationSpec

def fetchAllPages(
uri: Uri
): ZIO[CredentialDefinitionController & AuthenticatorAuthorizer[BaseEntity], Throwable, List[
): ZIO[CredentialDefinitionController & AuthenticatorWithAuthZ[BaseEntity], Throwable, List[
CredentialDefinitionResponsePage
]] = {
for {
controller <- ZIO.service[CredentialDefinitionController]
authenticator <- ZIO.service[AuthenticatorAuthorizer[BaseEntity]]
authenticator <- ZIO.service[AuthenticatorWithAuthZ[BaseEntity]]
backend = httpBackend(controller, authenticator)
response: CredentialDefinitionResponsePageType <-
for {
Expand Down Expand Up @@ -79,7 +79,7 @@ object CredentialDefinitionLookupAndPaginationSpec
for {
_ <- deleteAllCredentialDefinitions
controller <- ZIO.service[CredentialDefinitionController]
authenticator <- ZIO.service[AuthenticatorAuthorizer[BaseEntity]]
authenticator <- ZIO.service[AuthenticatorWithAuthZ[BaseEntity]]
backend = httpBackend(controller, authenticator)

inputs <- Generator.credentialDefinitionInput.runCollectN(10)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import io.iohk.atala.agent.walletapi.service.{ManagedDIDService, MockManagedDIDS
import io.iohk.atala.agent.walletapi.storage.GenericSecretStorage
import io.iohk.atala.api.http.ErrorResponse
import io.iohk.atala.castor.core.model.did.PrismDIDOperation
import io.iohk.atala.iam.authentication.AuthenticatorAuthorizer
import io.iohk.atala.iam.authentication.AuthenticatorWithAuthZ
import io.iohk.atala.iam.authentication.DefaultEntityAuthenticator
import io.iohk.atala.pollux.core.repository.CredentialDefinitionRepository
import io.iohk.atala.pollux.core.service.{
Expand Down Expand Up @@ -75,12 +75,12 @@ trait CredentialDefinitionTestTools extends PostgresTestContainerSupport {
)
)

val authenticatorLayer: TaskLayer[AuthenticatorAuthorizer[BaseEntity]] = DefaultEntityAuthenticator.layer
val authenticatorLayer: TaskLayer[AuthenticatorWithAuthZ[BaseEntity]] = DefaultEntityAuthenticator.layer

lazy val testEnvironmentLayer = ZLayer.makeSome[
ManagedDIDService,
CredentialDefinitionController & CredentialDefinitionRepository & CredentialDefinitionService &
PostgreSQLContainer & AuthenticatorAuthorizer[BaseEntity] & GenericSecretStorage
PostgreSQLContainer & AuthenticatorWithAuthZ[BaseEntity] & GenericSecretStorage
](
controllerLayer,
pgContainerLayer,
Expand All @@ -96,7 +96,7 @@ trait CredentialDefinitionTestTools extends PostgresTestContainerSupport {

def httpBackend(
controller: CredentialDefinitionController,
authenticator: AuthenticatorAuthorizer[BaseEntity]
authenticator: AuthenticatorWithAuthZ[BaseEntity]
) = {
val credentialDefinitionRegistryEndpoints = CredentialDefinitionRegistryServerEndpoints(controller, authenticator)

Expand Down Expand Up @@ -186,12 +186,12 @@ trait CredentialDefinitionGen {

def generateCredentialDefinitionsN(
count: Int
): ZIO[CredentialDefinitionController & AuthenticatorAuthorizer[BaseEntity], Throwable, List[
): ZIO[CredentialDefinitionController & AuthenticatorWithAuthZ[BaseEntity], Throwable, List[
CredentialDefinitionInput
]] =
for {
controller <- ZIO.service[CredentialDefinitionController]
authenticator <- ZIO.service[AuthenticatorAuthorizer[BaseEntity]]
authenticator <- ZIO.service[AuthenticatorWithAuthZ[BaseEntity]]
backend = httpBackend(controller, authenticator)
inputs <- Generator.credentialDefinitionInput.runCollectN(count)
_ <- inputs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package io.iohk.atala.pollux.schema
import io.iohk.atala.agent.walletapi.model.BaseEntity
import io.iohk.atala.api.http.ErrorResponse
import io.iohk.atala.container.util.MigrationAspects.*
import io.iohk.atala.iam.authentication.AuthenticatorAuthorizer
import io.iohk.atala.iam.authentication.AuthenticatorWithAuthZ
import io.iohk.atala.pollux.core.model.schema.`type`.anoncred.AnoncredSchemaSerDesV1
import io.iohk.atala.pollux.core.model.schema.`type`.{AnoncredSchemaType, CredentialJsonSchemaType}
import io.iohk.atala.pollux.credentialschema.*
Expand Down Expand Up @@ -51,7 +51,7 @@ object CredentialSchemaAnoncredSpec extends ZIOSpecDefault with CredentialSchema
+ wrapSpec(unsupportedSchemaSpec)
+ wrapSpec(wrongSchemaSpec)

private def wrapSpec(spec: Spec[CredentialSchemaController & AuthenticatorAuthorizer[BaseEntity], Throwable]) = {
private def wrapSpec(spec: Spec[CredentialSchemaController & AuthenticatorWithAuthZ[BaseEntity], Throwable]) = {
(spec
@@ nondeterministic @@ sequential @@ timed @@ migrateEach(
schema = "public",
Expand All @@ -64,7 +64,7 @@ object CredentialSchemaAnoncredSpec extends ZIOSpecDefault with CredentialSchema
private val schemaCreateAndGetOperationsSpec = {
def getSchemaZIO(uuid: UUID) = for {
controller <- ZIO.service[CredentialSchemaController]
authenticator <- ZIO.service[AuthenticatorAuthorizer[BaseEntity]]
authenticator <- ZIO.service[AuthenticatorWithAuthZ[BaseEntity]]
backend = httpBackend(controller, authenticator)
response <- basicRequest
.get(credentialSchemaUriBase.addPath(uuid.toString))
Expand Down Expand Up @@ -131,7 +131,7 @@ object CredentialSchemaAnoncredSpec extends ZIOSpecDefault with CredentialSchema
private def createResponse[B: JsonDecoder](schemaType: String) = {
for {
controller <- ZIO.service[CredentialSchemaController]
authenticator <- ZIO.service[AuthenticatorAuthorizer[BaseEntity]]
authenticator <- ZIO.service[AuthenticatorWithAuthZ[BaseEntity]]
backend = httpBackend(controller, authenticator)
response <-
basicRequest
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import io.iohk.atala.agent.walletapi.model.BaseEntity
import io.iohk.atala.agent.walletapi.service.ManagedDIDService
import io.iohk.atala.api.http.ErrorResponse
import io.iohk.atala.container.util.MigrationAspects.*
import io.iohk.atala.iam.authentication.AuthenticatorAuthorizer
import io.iohk.atala.iam.authentication.AuthenticatorWithAuthZ
import io.iohk.atala.pollux.core.model.schema.`type`.CredentialJsonSchemaType
import io.iohk.atala.pollux.credentialschema.*
import io.iohk.atala.pollux.credentialschema.controller.CredentialSchemaController
Expand Down Expand Up @@ -66,7 +66,7 @@ object CredentialSchemaBasicSpec extends ZIOSpecDefault with CredentialSchemaTes
val backendZIO =
for {
controller <- ZIO.service[CredentialSchemaController]
authenticator <- ZIO.service[AuthenticatorAuthorizer[BaseEntity]]
authenticator <- ZIO.service[AuthenticatorWithAuthZ[BaseEntity]]
} yield httpBackend(controller, authenticator)

def createSchemaResponseZIO = for {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import io.iohk.atala.agent.walletapi.model.BaseEntity
import io.iohk.atala.agent.walletapi.service.MockManagedDIDService
import io.iohk.atala.api.http.ErrorResponse
import io.iohk.atala.container.util.MigrationAspects.migrate
import io.iohk.atala.iam.authentication.AuthenticatorAuthorizer
import io.iohk.atala.iam.authentication.AuthenticatorWithAuthZ
import io.iohk.atala.pollux.credentialschema.*
import io.iohk.atala.pollux.credentialschema.controller.CredentialSchemaController
import sttp.client3.DeserializationException
Expand All @@ -27,7 +27,7 @@ object CredentialSchemaFailureSpec extends ZIOSpecDefault with CredentialSchemaT
test("create the schema with wrong json body returns BadRequest as json") {
for {
controller <- ZIO.service[CredentialSchemaController]
authenticator <- ZIO.service[AuthenticatorAuthorizer[BaseEntity]]
authenticator <- ZIO.service[AuthenticatorWithAuthZ[BaseEntity]]
backend = httpBackend(controller, authenticator)
response: SchemaBadRequestResponse <- basicRequest
.post(credentialSchemaUriBase)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package io.iohk.atala.pollux.schema
import com.dimafeng.testcontainers.PostgreSQLContainer
import io.iohk.atala.agent.walletapi.model.BaseEntity
import io.iohk.atala.container.util.MigrationAspects.migrate
import io.iohk.atala.iam.authentication.AuthenticatorAuthorizer
import io.iohk.atala.iam.authentication.AuthenticatorWithAuthZ
import io.iohk.atala.pollux.credentialschema.*
import io.iohk.atala.pollux.credentialschema.controller.CredentialSchemaController
import io.iohk.atala.pollux.credentialschema.http.{
Expand All @@ -28,12 +28,12 @@ object CredentialSchemaLookupAndPaginationSpec

def fetchAllPages(
uri: Uri
): ZIO[CredentialSchemaController & AuthenticatorAuthorizer[BaseEntity], Throwable, List[
): ZIO[CredentialSchemaController & AuthenticatorWithAuthZ[BaseEntity], Throwable, List[
CredentialSchemaResponsePage
]] = {
for {
controller <- ZIO.service[CredentialSchemaController]
authenticator <- ZIO.service[AuthenticatorAuthorizer[BaseEntity]]
authenticator <- ZIO.service[AuthenticatorWithAuthZ[BaseEntity]]
backend = httpBackend(controller, authenticator)
response: SchemaPageResponse <- basicRequest
.get(uri)
Expand Down Expand Up @@ -76,7 +76,7 @@ object CredentialSchemaLookupAndPaginationSpec
for {
_ <- deleteAllCredentialSchemas
controller <- ZIO.service[CredentialSchemaController]
authenticator <- ZIO.service[AuthenticatorAuthorizer[BaseEntity]]
authenticator <- ZIO.service[AuthenticatorWithAuthZ[BaseEntity]]
backend = httpBackend(controller, authenticator)

inputs <- Generator.schemaInput.runCollectN(101)
Expand Down
Loading

0 comments on commit 02df2ad

Please sign in to comment.