Skip to content

capitalize is deprecated #113

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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: 12 additions & 5 deletions oauth/src/OAuthClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class GoogleOAuthClient(httpClient: HttpClient): OAuthClient(
val res = fetchProfileResponse(token)
val email = Email(res.getString("email"))
return UserProfile(provider, res.getString("id"), email,
res.getOrNull("givenName") ?: email.value.substringBefore("@").capitalize(), res.getOrNull("familyName") ?: "",
res.getOrNull("givenName") ?: email.extractName(), res.getOrNull("familyName") ?: "",
res.getOrNull<String>("picture")?.let { URI(it) }, res.getLocale())
}
}
Expand All @@ -82,8 +82,8 @@ class MicrosoftOAuthClient(httpClient: HttpClient): OAuthClient(
) {
override suspend fun profile(token: OAuthTokenResponse, exchange: HttpExchange): UserProfile {
val res = fetchProfileResponse(token)
val email = res.getOrNull("mail") ?: res.getOrNull<String>("userPrincipalName") ?: error("Cannot obtain user's email")
return UserProfile(provider, res.getString("id"), Email(email), res.getOrNull("givenName") ?: email.substringBefore("@").capitalize(), res.getOrNull("surname") ?: "",
val email = Email(res.getOrNull("mail") ?: res.getOrNull<String>("userPrincipalName") ?: error("Cannot obtain user's email"))
return UserProfile(provider, res.getString("id"), email, res.getOrNull("givenName") ?: email.extractName(), res.getOrNull("surname") ?: "",
locale = res.getLocale("preferredLanguage"))
}
}
Expand All @@ -102,7 +102,7 @@ class FacebookOAuthClient(httpClient: HttpClient): OAuthClient(
val avatarExists = avatarData?.getOrNull<Boolean>("is_silhouette") != true
val email = Email(res.getString("email"))
return UserProfile(provider, res.getString("id"),
email, res.getOrNull("firstName") ?: email.value.substringBefore("@").capitalize(), res.getOrNull("lastName") ?: "",
email, res.getOrNull("firstName") ?: email.extractName(), res.getOrNull("lastName") ?: "",
avatarData?.getOrNull<String>("url")?.takeIf { avatarExists }?.let { URI(it) },
res.getLocale())
}
Expand All @@ -119,7 +119,7 @@ class AppleOAuthClient(httpClient: HttpClient): OAuthClient(
override suspend fun profile(token: OAuthTokenResponse, exchange: HttpExchange): UserProfile {
val email = token.idToken!!.payload.email!!
val user = exchange.bodyParams["user"]?.let { http.json.parse<AppleUserProfile>(it.toString()) }
return UserProfile(provider, token.idToken.payload.subject, email, user?.name?.firstName ?: email.value.substringBefore("@").capitalize(), user?.name?.lastName ?: "")
return UserProfile(provider, token.idToken.payload.subject, email, user?.name?.firstName ?: email.extractName(), user?.name?.lastName ?: "")
}

data class AppleUserProfile(val name: AppleUserName, val email: Email)
Expand All @@ -128,3 +128,10 @@ class AppleOAuthClient(httpClient: HttpClient): OAuthClient(

data class OAuthTokenResponse(val accessToken: String, val expiresIn: Int, val scope: String? = null, val tokenType: String? = null, val idToken: JWT? = null, val refreshToken: String? = null)
data class UserProfile(val provider: String, override val id: String, override val email: Email, override val firstName: String, override val lastName: String, val avatarUrl: URI? = null, val locale: Locale? = null): OAuthUser

internal fun Email.extractName(): String {
val localPart = value.substringBefore("@")
return localPart.replaceFirstChar {
if (it.isLowerCase()) it.titlecase(Locale.getDefault()) else it.toString()
}
}
32 changes: 32 additions & 0 deletions oauth/test/OAuthClientKtTest.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import ch.tutteli.atrium.api.fluent.en_GB.toEqual
import ch.tutteli.atrium.api.verbs.expect
import klite.Email
import klite.oauth.extractName
import org.junit.jupiter.params.ParameterizedTest
import org.junit.jupiter.params.provider.Arguments
import org.junit.jupiter.params.provider.MethodSource
import java.util.stream.Stream

class OAuthClientKtTest {

@ParameterizedTest
@MethodSource("emailTestCases")
fun `use capitalized email local part as name`(emailAddress: String, expectedName: String) {
val email = Email(emailAddress)
expect(email.extractName()).toEqual(expectedName)
}

companion object {
@JvmStatic
fun emailTestCases(): Stream<Arguments> =
Stream.of(
Arguments.of("[email protected]", "Foo"),
Arguments.of("[email protected]", "Foo"),
Arguments.of("[email protected]", "John.doe"),
Arguments.of("[email protected]", "Alice"),
Arguments.of("[email protected]", "Bob-smith"),
Arguments.of("[email protected]", "1234"),
Arguments.of("[email protected]", "Uppercase")
)
}
}