|
| 1 | +package org.polyvariant.sttp.oauth2.json.ziojson |
| 2 | + |
| 3 | +import org.polyvariant.sttp.oauth2.ClientCredentialsToken.AccessTokenResponse |
| 4 | +import org.polyvariant.sttp.oauth2.ExtendedOAuth2TokenResponse |
| 5 | +import org.polyvariant.sttp.oauth2.Introspection.Audience |
| 6 | +import org.polyvariant.sttp.oauth2.Introspection.SeqAudience |
| 7 | +import org.polyvariant.sttp.oauth2.Introspection.StringAudience |
| 8 | +import org.polyvariant.sttp.oauth2.Introspection.TokenIntrospectionResponse |
| 9 | +import org.polyvariant.sttp.oauth2.OAuth2TokenResponse |
| 10 | +import org.polyvariant.sttp.oauth2.RefreshTokenResponse |
| 11 | +import org.polyvariant.sttp.oauth2.Secret |
| 12 | +import org.polyvariant.sttp.oauth2.TokenUserDetails |
| 13 | +import org.polyvariant.sttp.oauth2.UserInfo |
| 14 | +import org.polyvariant.sttp.oauth2.common.Error.OAuth2Error |
| 15 | +import org.polyvariant.sttp.oauth2.common.Scope |
| 16 | +import org.polyvariant.sttp.oauth2.json.{JsonDecoder => OAuth2JsonDecoder} |
| 17 | +import zio.json.SnakeCase |
| 18 | +import zio.json._ |
| 19 | +import zio.json.jsonMemberNames |
| 20 | + |
| 21 | +import java.time.Instant |
| 22 | +import scala.concurrent.duration.DurationLong |
| 23 | +import scala.concurrent.duration.FiniteDuration |
| 24 | + |
| 25 | +trait ZioJsonDecoders { |
| 26 | + import ZioJsonDecoders._ |
| 27 | + |
| 28 | + implicit def jsonDecoder[A]( |
| 29 | + implicit decoder: JsonDecoder[A] |
| 30 | + ): OAuth2JsonDecoder[A] = |
| 31 | + (data: String) => decoder.decodeJson(data).left.map(msg => OAuth2JsonDecoder.Error(msg)) |
| 32 | + |
| 33 | + implicit val userInfoDecoder: JsonDecoder[UserInfo] = |
| 34 | + userInfoRawDecoder.map { raw => |
| 35 | + UserInfo( |
| 36 | + raw.sub, |
| 37 | + raw.name, |
| 38 | + raw.givenName, |
| 39 | + raw.familyName, |
| 40 | + raw.jobTitle, |
| 41 | + raw.domain, |
| 42 | + raw.preferredUsername, |
| 43 | + raw.email, |
| 44 | + raw.emailVerified, |
| 45 | + raw.locale, |
| 46 | + raw.sites.getOrElse(Nil), |
| 47 | + raw.banners.getOrElse(Nil), |
| 48 | + raw.regions.getOrElse(Nil), |
| 49 | + raw.fulfillmentContexts.getOrElse(Nil) |
| 50 | + ) |
| 51 | + } |
| 52 | + |
| 53 | + implicit val accessTokenResponseDecoder: JsonDecoder[AccessTokenResponse] = |
| 54 | + accessTokenResponseRawDecoder.mapOrFail { raw => |
| 55 | + if (raw.tokenType.equalsIgnoreCase("Bearer")) |
| 56 | + Right(AccessTokenResponse(raw.accessToken, raw.domain, raw.expiresIn, raw.scope)) |
| 57 | + else |
| 58 | + Left(s"Error while decoding '.token_type': value '${raw.tokenType}' is not equal to 'Bearer'") |
| 59 | + } |
| 60 | + |
| 61 | + implicit val oAuth2ErrorDecoder: JsonDecoder[OAuth2Error] = |
| 62 | + oAuth2ErrorRawDecoder.map { raw => |
| 63 | + OAuth2Error.fromErrorTypeAndDescription(raw.error, raw.errorDescription) |
| 64 | + } |
| 65 | + |
| 66 | + implicit val oAuth2TokenResponseDecoder: JsonDecoder[OAuth2TokenResponse] = |
| 67 | + oAuth2TokenResponseDecoderImpl |
| 68 | + |
| 69 | + implicit val extendedOAuth2TokenResponseDecoder: JsonDecoder[ExtendedOAuth2TokenResponse] = |
| 70 | + extendedOAuth2TokenResponseDecoderImpl |
| 71 | + |
| 72 | + implicit val tokenIntrospectionResponseDecoder: JsonDecoder[TokenIntrospectionResponse] = |
| 73 | + tokenIntrospectionResponseDecoderImpl |
| 74 | + |
| 75 | + implicit val refreshTokenResponseDecoder: JsonDecoder[RefreshTokenResponse] = |
| 76 | + refreshTokenResponseDecoderImpl |
| 77 | + |
| 78 | +} |
| 79 | + |
| 80 | +object ZioJsonDecoders { |
| 81 | + |
| 82 | + private[ziojson] implicit val secretStringDecoder: JsonDecoder[Secret[String]] = |
| 83 | + JsonDecoder.string.map(Secret(_)) |
| 84 | + |
| 85 | + private[ziojson] implicit val secondsDecoder: JsonDecoder[FiniteDuration] = |
| 86 | + JsonDecoder.long.map(_.seconds) |
| 87 | + |
| 88 | + private[ziojson] implicit val instantDecoder: JsonDecoder[Instant] = |
| 89 | + JsonDecoder.long.map(Instant.ofEpochSecond) |
| 90 | + |
| 91 | + private[ziojson] implicit val scopeDecoder: JsonDecoder[Scope] = |
| 92 | + JsonDecoder.string.mapOrFail { value => |
| 93 | + Scope.from(value).left.map(identity) |
| 94 | + } |
| 95 | + |
| 96 | + private[ziojson] implicit val optionScopeDecoder: JsonDecoder[Option[Scope]] = |
| 97 | + JsonDecoder.option[String].mapOrFail { |
| 98 | + case None | Some("") => Right(None) |
| 99 | + case Some(value) => Scope.from(value).map(Some(_)).left.map(identity) |
| 100 | + } |
| 101 | + |
| 102 | + private[ziojson] implicit val audienceDecoder: JsonDecoder[Audience] = |
| 103 | + JsonDecoder |
| 104 | + .string |
| 105 | + .map(StringAudience(_)) |
| 106 | + .orElse( |
| 107 | + JsonDecoder.list[String].map(seq => SeqAudience(seq)) |
| 108 | + ) |
| 109 | + |
| 110 | + private[ziojson] implicit val codecConfig: JsonCodecConfiguration = |
| 111 | + JsonCodecConfiguration(fieldNameMapping = SnakeCase) |
| 112 | + |
| 113 | + private[ziojson] implicit val tokenUserDetailsDecoder: JsonDecoder[TokenUserDetails] = |
| 114 | + DeriveJsonDecoder.gen[TokenUserDetails] |
| 115 | + |
| 116 | + private[ziojson] val oAuth2TokenResponseDecoderImpl: JsonDecoder[OAuth2TokenResponse] = |
| 117 | + DeriveJsonDecoder.gen[OAuth2TokenResponse] |
| 118 | + |
| 119 | + private[ziojson] val extendedOAuth2TokenResponseDecoderImpl: JsonDecoder[ExtendedOAuth2TokenResponse] = |
| 120 | + DeriveJsonDecoder.gen[ExtendedOAuth2TokenResponse] |
| 121 | + |
| 122 | + private[ziojson] val tokenIntrospectionResponseDecoderImpl: JsonDecoder[TokenIntrospectionResponse] = |
| 123 | + DeriveJsonDecoder.gen[TokenIntrospectionResponse] |
| 124 | + |
| 125 | + private[ziojson] val refreshTokenResponseDecoderImpl: JsonDecoder[RefreshTokenResponse] = |
| 126 | + DeriveJsonDecoder.gen[RefreshTokenResponse] |
| 127 | + |
| 128 | + // Raw case classes for special handling |
| 129 | + |
| 130 | + // Using Raw copy because the original UserInfo has list fields with default values (Nil) |
| 131 | + // that need special handling to map Option[List[String]] -> List[String] |
| 132 | + @jsonMemberNames(SnakeCase) |
| 133 | + private final case class UserInfoRaw( |
| 134 | + sub: Option[String], |
| 135 | + name: Option[String], |
| 136 | + givenName: Option[String], |
| 137 | + familyName: Option[String], |
| 138 | + jobTitle: Option[String], |
| 139 | + domain: Option[String], |
| 140 | + preferredUsername: Option[String], |
| 141 | + email: Option[String], |
| 142 | + emailVerified: Option[Boolean], |
| 143 | + locale: Option[String], |
| 144 | + sites: Option[List[String]], |
| 145 | + banners: Option[List[String]], |
| 146 | + regions: Option[List[String]], |
| 147 | + fulfillmentContexts: Option[List[String]] |
| 148 | + ) |
| 149 | + |
| 150 | + private val userInfoRawDecoder: JsonDecoder[UserInfoRaw] = |
| 151 | + DeriveJsonDecoder.gen[UserInfoRaw] |
| 152 | + |
| 153 | + // Using Raw copy because we need to validate tokenType = "Bearer" |
| 154 | + @jsonMemberNames(SnakeCase) |
| 155 | + private final case class AccessTokenResponseRaw( |
| 156 | + accessToken: Secret[String], |
| 157 | + domain: Option[String], |
| 158 | + expiresIn: FiniteDuration, |
| 159 | + scope: Option[Scope], |
| 160 | + tokenType: String |
| 161 | + ) |
| 162 | + |
| 163 | + private val accessTokenResponseRawDecoder: JsonDecoder[AccessTokenResponseRaw] = |
| 164 | + DeriveJsonDecoder.gen[AccessTokenResponseRaw] |
| 165 | + |
| 166 | + // Using Raw copy because we need custom construction via fromErrorTypeAndDescription |
| 167 | + @jsonMemberNames(SnakeCase) |
| 168 | + private final case class OAuth2ErrorRaw( |
| 169 | + error: String, |
| 170 | + errorDescription: Option[String] |
| 171 | + ) |
| 172 | + |
| 173 | + private val oAuth2ErrorRawDecoder: JsonDecoder[OAuth2ErrorRaw] = |
| 174 | + DeriveJsonDecoder.gen[OAuth2ErrorRaw] |
| 175 | + |
| 176 | +} |
0 commit comments