Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
16 commits
Select commit Hold shift + click to select a range
8fa0c6f
Bumped version to 0.0.30
andylamax Oct 8, 2021
ec0ecd5
[BF142](https://github.com/picortex/bitframe/issues/142) Refactor Sig…
andylamax Oct 10, 2021
50a1082
[BF142](https://github.com/picortex/bitframe/issues/142) Removed Lega…
andylamax Oct 10, 2021
c7b03dd
[BF142](https://github.com/picortex/bitframe/issues/142) Purged our t…
andylamax Oct 10, 2021
064cd06
[[BF143]](https://github.com/picortex/bitframe/issues/143) Response i…
andylamax Oct 10, 2021
e63f1d5
[[BF144]](https://github.com/picortex/bitframe/issues/144) Fix Cross …
andylamax Oct 10, 2021
0aa5f0d
Fixed failing CI https://github.com/picortex/bitframe/pull/145/checks…
andylamax Oct 10, 2021
341a4ab
Merge pull request #145 from picortex/master-dev-anderson
andylamax Oct 10, 2021
7b814ac
Intoduced the monitors service
andylamax Oct 14, 2021
15a6a79
[[PM121]](https://github.com/picortex/bitframe/issues/121) Persist th…
andylamax Oct 14, 2021
2d2d5e9
Merge pull request #147 from picortex/master-dev-anderson
andylamax Oct 15, 2021
9d8703c
[[PM123]](https://github.com/picortex/bitframe/issues/123) Moved the …
andylamax Oct 15, 2021
89011c9
Fixed failing CI https://github.com/picortex/bitframe/runs/3902471828…
andylamax Oct 15, 2021
3ca82b0
[[PM151]](https://github.com/picortex/bitframe/issues/151) Automatica…
andylamax Oct 15, 2021
9473a67
Fixed failing CI https://github.com/picortex/bitframe/runs/3903120131…
andylamax Oct 15, 2021
8bd330c
Merge pull request #152 from picortex/master-dev-anderson
andylamax Oct 15, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
# 0.0.30

## PiMonitor Client SDK Core

- [[PM121]](https://github.com/picortex/bitframe/issues/121) Persist the currently logged in Monitor
- [[PM142]](https://github.com/picortex/bitframe/issues/142) Refactor SignUpService to just use one method for signing up
- [[PM123]](https://github.com/picortex/bitframe/issues/123) Moved the sing in/up event into the event bus
- [[PM151]](https://github.com/picortex/bitframe/issues/151) Automatically sign in user after registration

## Bitframe

- [[BF143]](https://github.com/picortex/bitframe/issues/143) Response is not properly returning the cause of an error

## Bitframe Server

- [[BF144]](https://github.com/picortex/bitframe/issues/144) Fix Cross Origin Resource Sharing Issue

# 0.0.29

## PiMonitor Client SDK Core
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,21 @@ import kotlin.js.JsExport
sealed class Session {
object Unknown : Session()

class SignedIn(
data class SignedIn(
val app: App,
val space: Space,
val user: User,
val user: User
) : Session()

class Conundrum(
data class Conundrum(
val app: App,
val spaces: List<Space>,
val user: User
) : Session()

class SignedOut(
data class SignedOut(
val app: App,
val space: Space?,
val user: User?,
val user: User?
) : Session()
}
Original file line number Diff line number Diff line change
Expand Up @@ -122,15 +122,24 @@ sealed class Contacts {
}
}

/**
* Returns a [Set] of all contacts in their string representation
*/
fun mapEachToString() = when (this) {
None -> emptySet()
is Email -> setOf(email.value)
is Emails -> emails.map { it.value }.toSet()
is Phone -> setOf(phone.value)
is Phones -> phones.map { it.value }.toSet()
is EmailPhone -> setOf(email.value, phone.value)
is Mixed -> (emails.map { it.value } + phones.map { it.value }).toSet()
}

@Deprecated("In favour of mapToString().first())", replaceWith = ReplaceWith("mapToString().first()"))
fun firstValue() = firstValueOrNull() ?: throw RuntimeException("There are no Contacts inside the contacts container")

fun firstValueOrNull() = when (this) {
None -> null
is Email -> email.value
is Emails -> emails.firstOrNull()?.value
is Phone -> phone.value
is Phones -> phones.firstOrNull()?.value
is EmailPhone -> email.value
is Mixed -> emails.firstOrNull()?.value
}
@Deprecated("In favour of mapToString().firstOrNull()", replaceWith = ReplaceWith("mapToString().firstOrNull()"))
fun firstValueOrNull() = mapEachToString().firstOrNull()

fun contains(value: String) = mapEachToString().contains(value.lowercase())
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import kotlinx.serialization.Serializable
import kotlin.js.JsExport

@Serializable
class UserRef(
data class UserRef(
val uid: String,
val name: String,
val tag: String,
Expand Down
1 change: 1 addition & 0 deletions bitframe-authentication/services/core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ kotlin {
val commonMain by getting {
dependencies {
api(project(":bitframe-service-core"))
api(project(":bitframe-events-core"))
api(project(":bitframe-authentication-dao-core"))
api(asoft("live-core", vers.asoft.live))
api(asoft("later-ktx", vers.asoft.later))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,33 +1,75 @@
@file:JsExport

package bitframe.authentication.signin

import bitframe.authentication.apps.App
import bitframe.authentication.spaces.Space
import bitframe.authentication.users.User
import bitframe.events.Event
import bitframe.events.EventBus
import bitframe.service.config.ServiceConfig
import later.Later
import later.await
import later.later
import live.Live
import kotlin.js.JsExport
import kotlin.jvm.JvmField
import kotlin.jvm.JvmStatic

abstract class SignInService {
abstract class SignInService(
open val bus: EventBus,
open val config: ServiceConfig
) {
val session: Live<Session> = Live(Session.Unknown)
val currentSession get() = session.value
protected val scope get() = config.scope

companion object {
val session: Live<Session> = Live(Session.Unknown)
const val SIGN_IN_EVENT_ID = "bitframe.authentication.sign.in"
const val SIGN_OUT_EVENT_ID = "bitframe.authentication.sign.out"

val current get() = session.value
fun signInEvent(session: Session.SignedIn) = Event(SIGN_IN_EVENT_ID, session)
}

fun validate(credentials: SignInCredentials) {
require(credentials.alias.isNotEmpty()) { "loginId (i.e. email/phone/username), must not be empty" }
require(credentials.password.isNotEmpty()) { "Password must not be empty" }
}

abstract fun signIn(credentials: SignInCredentials): Later<LoginConundrum>
/**
* Do not call this method directly. Call [signIn] instead
*/
protected abstract fun executeSignIn(credentials: SignInCredentials): Later<LoginConundrum>

fun signIn(credentials: SignInCredentials): Later<LoginConundrum> = scope.later {
validate(credentials)
val conundrum = executeSignIn(credentials).await()
if (conundrum.spaces.size == 1) {
val (user, spaces) = conundrum
val s = Session.SignedIn(App(config.appId), spaces.first(), user)
session.value = s
bus.dispatch(signInEvent(s))
}
conundrum
}

/**
* Resolve a [LoginConundrum] by specifying which [Space] a user currently wants to log in
*
* This method should only be called when [signIn] returned a conundrum with at least two [LoginConundrum.spaces]
*/
abstract fun resolve(space: Space): Later<Session.SignedIn>
fun resolve(space: Space): Later<Session.SignedIn> = scope.later {
val error = IllegalStateException(
"""
You are attempting to resolve a non exiting conundrum,

Make sure you have tried to signIn and the result obtained was a LoginConundrum with more that one space
""".trimIndent()
)
when (val s = session.value) {
Session.Unknown -> throw error
is Session.SignedIn -> Session.SignedIn(App(config.appId), space, s.user)
is Session.Conundrum -> Session.SignedIn(App(config.appId), space, s.user)
is Session.SignedOut -> throw error
}.also {
session.value = it
bus.dispatch(signInEvent(it))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,24 @@ import bitframe.authentication.AuthenticationDaoProvider
import bitframe.authentication.apps.App
import bitframe.authentication.spaces.Space
import bitframe.authentication.spaces.SpacesDao
import bitframe.authentication.users.User
import bitframe.authentication.users.UsersDao
import bitframe.daos.conditions.contains
import bitframe.events.EventBus
import bitframe.service.config.ServiceConfig
import later.Later
import later.await
import later.later

class SignInServiceImpl(
open class SignInServiceImpl(
private val usersDao: UsersDao,
private val spacesDao: SpacesDao,
private val config: ServiceConfig
) : SignInService() {
constructor(provider: AuthenticationDaoProvider, config: ServiceConfig) : this(provider.users, provider.spaces, config)
override val config: ServiceConfig,
override val bus: EventBus
) : SignInService(bus, config) {
constructor(provider: AuthenticationDaoProvider, config: ServiceConfig, bus: EventBus) : this(provider.users, provider.spaces, config, bus)

private val scope = config.scope
override fun signIn(credentials: SignInCredentials): Later<LoginConundrum> = scope.later {
validate(credentials)
override fun executeSignIn(credentials: SignInCredentials): Later<LoginConundrum> = scope.later {
val matches = usersDao.all(where = "contacts" contains credentials.alias).await()
if (matches.isEmpty()) throw RuntimeException("User with loginId=${credentials.alias}, not found")
val match = matches.first()
Expand All @@ -35,20 +36,4 @@ class SignInServiceImpl(
}
}
}

override fun resolve(space: Space): Later<Session.SignedIn> = scope.later {
val error = IllegalStateException(
"""
You are attempting to resolve a non exiting conundrum,

Make sure you have tried to signIn and the result obtained was a LoginConundrum with more that one space
""".trimIndent()
)
when (val s = session.value) {
Session.Unknown -> throw error
is Session.SignedIn -> Session.SignedIn(App(config.appId), space, s.user)
is Session.Conundrum -> Session.SignedIn(App(config.appId), space, s.user)
is Session.SignedOut -> throw error
}.also { session.value = it }
}
}
Original file line number Diff line number Diff line change
@@ -1,48 +1,29 @@
package bitframe.authentication.signin

import bitframe.authentication.apps.App
import bitframe.authentication.spaces.Space
import bitframe.events.EventBus
import bitframe.response.response.decodeResponseFromString
import bitframe.service.MiniService
import bitframe.service.config.KtorClientConfiguration
import bitframe.service.utils.JsonContent
import io.ktor.client.features.*
import io.ktor.client.request.*
import io.ktor.content.*
import io.ktor.http.*
import io.ktor.client.statement.*
import kotlinx.serialization.json.Json
import later.Later
import later.later
import kotlin.jvm.JvmOverloads

class SignInServiceKtor @JvmOverloads constructor(
override val config: KtorClientConfiguration
) : SignInService(), MiniService {
private val path = config.url + "/api/authentication/sign-in"
private val http = config.http
private val scope = config.scope
override fun signIn(credentials: SignInCredentials): Later<LoginConundrum> = scope.later {
validate(credentials)
val json = http.post<String>(path) {
body = TextContent(
text = Json.encodeToString(SignInCredentials.serializer(), credentials),
contentType = ContentType.Application.Json
)
open class SignInServiceKtor(
override val config: KtorClientConfiguration,
override val bus: EventBus
) : SignInService(bus, config), MiniService {
private val path get() = config.url + "/api/authentication/sign-in"
private val http get() = config.http
override fun executeSignIn(credentials: SignInCredentials): Later<LoginConundrum> = scope.later {
val resp = try {
http.post(path) { body = JsonContent(credentials) }
} catch (exp: ClientRequestException) {
exp.response
}
Json.decodeResponseFromString(LoginConundrum.serializer(), json).response()
}

override fun resolve(space: Space): Later<Session.SignedIn> = scope.later {
val error = IllegalStateException(
"""
You are attempting to resolve a non exiting conundrum,

Make sure you have tried to signIn and the result obtained was a LoginConundrum with more that one space
""".trimIndent()
)
when (val s = session.value) {
Session.Unknown -> throw error
is Session.SignedIn -> Session.SignedIn(App(config.appId), space, s.user)
is Session.Conundrum -> Session.SignedIn(App(config.appId), space, s.user)
is Session.SignedOut -> throw error
}.also { session.value = it }
Json.decodeResponseFromString(LoginConundrum.serializer(), resp.readText()).response()
}
}
7 changes: 1 addition & 6 deletions bitframe-authentication/services/test/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,7 @@ kotlin {
api(asoft("expect-coroutines", vers.asoft.expect))
api(project(":bitframe-authentication-dao-inmemory"))
api(project(":pi-monitor-test-testing"))
}
}

val jvmMain by getting {
dependencies {

api(project(":bitframe-events-inmemory"))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package bitframe.authentication.signin

import bitframe.authentication.InMemoryAuthenticationDaoProvider
import bitframe.authentication.spaces.SpacesDaoInMemory
import bitframe.authentication.users.UsersDaoInMemory
import bitframe.events.InMemoryEventBus
import bitframe.service.config.KtorClientConfiguration
import expect.expect
import expect.expectFailure
Expand All @@ -19,9 +18,10 @@ import kotlin.test.Test
open class SignInServiceTest : IntegrationTest() {
val provider = InMemoryAuthenticationDaoProvider()
open val service: SignInService by lazy {
val bus = InMemoryEventBus()
when (val cfg = config) {
is KtorClientConfiguration -> SignInServiceKtor(cfg)
else -> SignInServiceImpl(provider, cfg)
is KtorClientConfiguration -> SignInServiceKtor(cfg, bus)
else -> SignInServiceImpl(provider, cfg, bus)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@ import bitframe.authentication.signin.SignInService
import bitframe.authentication.signin.SignInServiceImpl
import bitframe.authentication.signin.SignInServiceKtor
import bitframe.authentication.signin.SignInServiceTest
import bitframe.events.InMemoryEventBus
import bitframe.service.config.KtorClientConfiguration
import expect.expect
import kotlin.test.Test

class SignInServiceIntegrationTest : SignInServiceTest() {
override val service: SignInService by lazy {
val bus = InMemoryEventBus()
when (val cfg = config) {
is KtorClientConfiguration -> SignInServiceKtor(cfg)
else -> SignInServiceImpl(InMemoryAuthenticationDaoProvider(), cfg)
is KtorClientConfiguration -> SignInServiceKtor(cfg, bus)
else -> SignInServiceImpl(InMemoryAuthenticationDaoProvider(), cfg, bus)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import bitframe.authentication.signin.SignInServiceKtor
import bitframe.authentication.signin.SignInServiceTest
import bitframe.authentication.spaces.SpacesDaoInMemory
import bitframe.authentication.users.UsersDaoInMemory
import bitframe.events.InMemoryEventBus
import bitframe.service.config.KtorClientConfiguration
import bitframe.service.config.ServiceConfig
import expect.expect
Expand All @@ -16,7 +17,11 @@ import kotlin.test.Test

class SignInServiceUnitTest : SignInServiceTest() {
override val service: SignInService by lazy {
SignInServiceImpl(InMemoryAuthenticationDaoProvider(), ServiceConfig(APP_ID))
SignInServiceImpl(
provider = InMemoryAuthenticationDaoProvider(),
config = ServiceConfig(APP_ID),
bus = InMemoryEventBus()
)
}

@Test
Expand Down
Loading