1 /*
2  *  Make.org Core API
3  *  Copyright (C) 2021 Make.org
4  *
5  * This program is free software: you can redistribute it and/or modify
6  *  it under the terms of the GNU Affero General Public License as
7  *  published by the Free Software Foundation, either version 3 of the
8  *  License, or (at your option) any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU Affero General Public License for more details.
14  *
15  *  You should have received a copy of the GNU Affero General Public License
16  *  along with this program.  If not, see <https://www.gnu.org/licenses/>.
17  *
18  */
19 
20 package org.make.api.technical.auth
21 
22 import io.circe.{Decoder, Encoder}
23 import io.swagger.annotations.ApiModelProperty
24 import org.make.api.technical.auth.MakeDataHandler.{CreatedAtParameter, RefreshTokenExpirationParameter}
25 import scalaoauth2.provider.AccessToken
26 
27 import scala.annotation.meta.field
28 
29 final case class TokenResponse(
30   @(ApiModelProperty @field)(name = "token_type", required = true)
31   tokenType: String,
32   @(ApiModelProperty @field)(name = "access_token", example = "11111111-2222-3333-4444-555555555555", required = true)
33   accessToken: String,
34   @(ApiModelProperty @field)(name = "expires_in", example = "300", required = true)
35   expiresIn: Long,
36   @(ApiModelProperty @field)(name = "refresh_token", example = "11111111-2222-3333-4444-555555555555")
37   refreshToken: Option[String],
38   @(ApiModelProperty @field)(name = "refresh_expires_in", example = "1500", dataType = "int")
39   refreshExpiresIn: Option[Long],
40   @(ApiModelProperty @field)(name = "created_at", dataType = "dateTime", required = true)
41   createdAt: String
42 )
43 
44 object TokenResponse {
45   def fromAccessToken(tokenResult: AccessToken): TokenResponse = {
46     TokenResponse(
47       "Bearer",
48       tokenResult.token,
49       tokenResult.expiresIn.getOrElse(1L),
50       tokenResult.refreshToken,
51       tokenResult.params.get(RefreshTokenExpirationParameter).map(_.toLong),
52       tokenResult.params.getOrElse(CreatedAtParameter, "")
53     )
54   }
55 
56   implicit val encoder: Encoder[TokenResponse] = {
57     Encoder.forProduct6("token_type", "access_token", "expires_in", "refresh_token", "refresh_expires_in", "created_at") {
58       response =>
59         (
60           response.tokenType,
61           response.accessToken,
62           response.expiresIn,
63           response.refreshToken,
64           response.refreshExpiresIn,
65           response.createdAt
66         )
67     }
68   }
69 
70   implicit val decoder: Decoder[TokenResponse] = {
71     Decoder.forProduct6(
72       "token_type",
73       "access_token",
74       "expires_in",
75       "refresh_token",
76       "refresh_expires_in",
77       "created_at"
78     )(TokenResponse.apply)
79   }
80 }
Line Stmt Id Pos Tree Symbol Tests Code
46 29400 1881 - 2153 Apply org.make.api.technical.auth.TokenResponse.apply TokenResponse.apply("Bearer", tokenResult.token, tokenResult.expiresIn.getOrElse[Long](1L), tokenResult.refreshToken, tokenResult.params.get(org.make.api.technical.auth.MakeDataHandler.RefreshTokenExpirationParameter).map[Long](((x$1: String) => scala.Predef.augmentString(x$1).toLong)), tokenResult.params.getOrElse[String](org.make.api.technical.auth.MakeDataHandler.CreatedAtParameter, ""))
47 29832 1902 - 1910 Literal <nosymbol> "Bearer"
48 29440 1918 - 1935 Select scalaoauth2.provider.AccessToken.token tokenResult.token
49 28554 1943 - 1978 Apply scala.Option.getOrElse tokenResult.expiresIn.getOrElse[Long](1L)
50 29964 1986 - 2010 Select scalaoauth2.provider.AccessToken.refreshToken tokenResult.refreshToken
51 28335 2078 - 2086 Select scala.collection.StringOps.toLong scala.Predef.augmentString(x$1).toLong
51 29220 2041 - 2072 Select org.make.api.technical.auth.MakeDataHandler.RefreshTokenExpirationParameter org.make.api.technical.auth.MakeDataHandler.RefreshTokenExpirationParameter
51 30145 2018 - 2087 Apply scala.Option.map tokenResult.params.get(org.make.api.technical.auth.MakeDataHandler.RefreshTokenExpirationParameter).map[Long](((x$1: String) => scala.Predef.augmentString(x$1).toLong))
52 29250 2124 - 2142 Select org.make.api.technical.auth.MakeDataHandler.CreatedAtParameter org.make.api.technical.auth.MakeDataHandler.CreatedAtParameter
52 28439 2144 - 2146 Literal <nosymbol> ""
52 29837 2095 - 2147 Apply scala.collection.MapOps.getOrElse tokenResult.params.getOrElse[String](org.make.api.technical.auth.MakeDataHandler.CreatedAtParameter, "")
57 28560 2234 - 2246 Literal <nosymbol> "token_type"
57 29173 2331 - 2331 Select io.circe.Encoder.encodeString circe.this.Encoder.encodeString
57 28341 2278 - 2293 Literal <nosymbol> "refresh_token"
57 29950 2331 - 2331 ApplyToImplicitArgs io.circe.Encoder.encodeOption circe.this.Encoder.encodeOption[Long](circe.this.Encoder.encodeLong)
57 29259 2331 - 2331 Select io.circe.Encoder.encodeString circe.this.Encoder.encodeString
57 30183 2331 - 2331 Select io.circe.Encoder.encodeString circe.this.Encoder.encodeString
57 28475 2331 - 2331 Select io.circe.Encoder.encodeLong circe.this.Encoder.encodeLong
57 29034 2331 - 2331 ApplyToImplicitArgs io.circe.Encoder.encodeOption circe.this.Encoder.encodeOption[String](circe.this.Encoder.encodeString)
57 28394 2214 - 2567 ApplyToImplicitArgs io.circe.ProductEncoders.forProduct6 io.circe.Encoder.forProduct6[org.make.api.technical.auth.TokenResponse, String, String, Long, Option[String], Option[Long], String]("token_type", "access_token", "expires_in", "refresh_token", "refresh_expires_in", "created_at")(((response: org.make.api.technical.auth.TokenResponse) => scala.Tuple6.apply[String, String, Long, Option[String], Option[Long], String](response.tokenType, response.accessToken, response.expiresIn, response.refreshToken, response.refreshExpiresIn, response.createdAt)))(circe.this.Encoder.encodeString, circe.this.Encoder.encodeString, circe.this.Encoder.encodeLong, circe.this.Encoder.encodeOption[String](circe.this.Encoder.encodeString), circe.this.Encoder.encodeOption[Long](circe.this.Encoder.encodeLong), circe.this.Encoder.encodeString)
57 30182 2295 - 2315 Literal <nosymbol> "refresh_expires_in"
57 29204 2264 - 2276 Literal <nosymbol> "expires_in"
57 29940 2248 - 2262 Literal <nosymbol> "access_token"
57 29906 2331 - 2331 Select io.circe.Encoder.encodeString circe.this.Encoder.encodeString
57 29254 2317 - 2329 Literal <nosymbol> "created_at"
57 28671 2331 - 2331 Select io.circe.Encoder.encodeLong circe.this.Encoder.encodeLong
59 28392 2359 - 2561 Apply scala.Tuple6.apply scala.Tuple6.apply[String, String, Long, Option[String], Option[Long], String](response.tokenType, response.accessToken, response.expiresIn, response.refreshToken, response.refreshExpiresIn, response.createdAt)
60 28440 2371 - 2389 Select org.make.api.technical.auth.TokenResponse.tokenType response.tokenType
61 29901 2401 - 2421 Select org.make.api.technical.auth.TokenResponse.accessToken response.accessToken
62 29404 2433 - 2451 Select org.make.api.technical.auth.TokenResponse.expiresIn response.expiresIn
63 28666 2463 - 2484 Select org.make.api.technical.auth.TokenResponse.refreshToken response.refreshToken
64 29947 2496 - 2521 Select org.make.api.technical.auth.TokenResponse.refreshExpiresIn response.refreshExpiresIn
65 29211 2533 - 2551 Select org.make.api.technical.auth.TokenResponse.createdAt response.createdAt
72 30101 2655 - 2667 Literal <nosymbol> "token_type"
73 29360 2675 - 2689 Literal <nosymbol> "access_token"
74 28481 2697 - 2709 Literal <nosymbol> "expires_in"
75 29877 2717 - 2732 Literal <nosymbol> "refresh_token"
76 29040 2740 - 2760 Literal <nosymbol> "refresh_expires_in"
77 28634 2768 - 2780 Literal <nosymbol> "created_at"
78 28605 2786 - 2786 Select io.circe.Decoder.decodeString circe.this.Decoder.decodeString
78 29047 2786 - 2786 ApplyToImplicitArgs io.circe.Decoder.decodeOption circe.this.Decoder.decodeOption[Long](circe.this.Decoder.decodeLong)
78 29178 2786 - 2786 Select io.circe.Decoder.decodeString circe.this.Decoder.decodeString
78 28454 2786 - 2786 ApplyToImplicitArgs io.circe.Decoder.decodeOption circe.this.Decoder.decodeOption[String](circe.this.Decoder.decodeString)
78 29943 2628 - 2807 ApplyToImplicitArgs io.circe.ProductDecoders.forProduct6 io.circe.Decoder.forProduct6[org.make.api.technical.auth.TokenResponse, String, String, Long, Option[String], Option[Long], String]("token_type", "access_token", "expires_in", "refresh_token", "refresh_expires_in", "created_at")(((tokenType: String, accessToken: String, expiresIn: Long, refreshToken: Option[String], refreshExpiresIn: Option[Long], createdAt: String) => TokenResponse.apply(tokenType, accessToken, expiresIn, refreshToken, refreshExpiresIn, createdAt)))(circe.this.Decoder.decodeString, circe.this.Decoder.decodeString, circe.this.Decoder.decodeLong, circe.this.Decoder.decodeOption[String](circe.this.Decoder.decodeString), circe.this.Decoder.decodeOption[Long](circe.this.Decoder.decodeLong), circe.this.Decoder.decodeString)
78 29979 2787 - 2806 Apply org.make.api.technical.auth.TokenResponse.apply akka.http.scaladsl.testkit.routetest TokenResponse.apply(tokenType, accessToken, expiresIn, refreshToken, refreshExpiresIn, createdAt)
78 29883 2786 - 2786 Select io.circe.Decoder.decodeLong circe.this.Decoder.decodeLong
78 29745 2786 - 2786 Select io.circe.Decoder.decodeLong circe.this.Decoder.decodeLong
78 29308 2786 - 2786 Select io.circe.Decoder.decodeString circe.this.Decoder.decodeString
78 28294 2786 - 2786 Select io.circe.Decoder.decodeString circe.this.Decoder.decodeString