Skip to content

Commit

Permalink
support for missing locale
Browse files Browse the repository at this point in the history
  • Loading branch information
angryziber committed Mar 14, 2024
1 parent 20360d6 commit c85d582
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* core: Registry and TypedHttpClient moved from server module, so that JsonHttpClient can be used without the server
* server: HttpExchange.path<>() and query<>() now allow for automatic String conversion into value types
* jobs: deprecated non-Duration schedule() methods
* oauth: support for missing first or last names
* oauth: support for missing first or last names or locale

# 1.6.6
* core: Converter will force initialize companion objects to better support `init { Converter.use {...} }`
Expand Down
8 changes: 5 additions & 3 deletions oauth/src/OAuthClient.kt
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ abstract class OAuthClient(scope: String, authUrl: String, tokenUrl: String, pro
protected suspend fun fetchProfileResponse(token: OAuthTokenResponse): JsonNode = http.get(profileUrl) { authBearer(token.accessToken) }

abstract suspend fun profile(token: OAuthTokenResponse, exchange: HttpExchange): UserProfile

protected fun JsonNode.getLocale(key: String = "locale") = getOrNull<String>(key)?.let { Locale.forLanguageTag(it) }
}

/** https://console.cloud.google.com/apis/credentials */
Expand All @@ -66,7 +68,7 @@ class GoogleOAuthClient(httpClient: HttpClient): OAuthClient(
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<String>("picture")?.let { URI(it) }, Locale.forLanguageTag(res.getString("locale")))
res.getOrNull<String>("picture")?.let { URI(it) }, res.getLocale())
}
}

Expand All @@ -82,7 +84,7 @@ class MicrosoftOAuthClient(httpClient: HttpClient): OAuthClient(
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") ?: "",
locale = Locale.forLanguageTag(res.getString("preferredLanguage")))
locale = res.getLocale("preferredLanguage"))
}
}

Expand All @@ -102,7 +104,7 @@ class FacebookOAuthClient(httpClient: HttpClient): OAuthClient(
return UserProfile(provider, res.getString("id"),
email, res.getOrNull("firstName") ?: email.value.substringBefore("@").capitalize(), res.getOrNull("lastName") ?: "",
avatarData?.getOrNull<String>("url")?.takeIf { avatarExists }?.let { URI(it) },
Locale.forLanguageTag(res.getString("locale")))
res.getLocale())
}
}

Expand Down

0 comments on commit c85d582

Please sign in to comment.