-
Notifications
You must be signed in to change notification settings - Fork 640
/
Copy pathOAuth2Srv.scala
154 lines (139 loc) · 5.93 KB
/
OAuth2Srv.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
package services
import javax.inject.{Inject, Singleton}
import akka.stream.Materializer
import org.elastic4play.services.{AuthContext, AuthSrv}
import org.elastic4play.{AuthenticationError, AuthorizationError, OAuth2Redirect}
import play.api.http.Status
import play.api.libs.json.{JsObject, JsValue}
import play.api.libs.ws.WSClient
import play.api.mvc.RequestHeader
import play.api.{Configuration, Logger}
import services.mappers.UserMapper
import scala.concurrent.{ExecutionContext, Future}
case class OAuth2Config(
clientId: String,
clientSecret: String,
redirectUri: String,
responseType: String,
grantType: String,
authorizationUrl: String,
tokenUrl: String,
userUrl: String,
scope: String,
autocreate: Boolean
)
object OAuth2Config {
def apply(configuration: Configuration): Option[OAuth2Config] =
for {
clientId ← configuration.getOptional[String]("auth.oauth2.clientId")
clientSecret ← configuration.getOptional[String]("auth.oauth2.clientSecret")
redirectUri ← configuration.getOptional[String]("auth.oauth2.redirectUri")
responseType ← configuration.getOptional[String]("auth.oauth2.responseType")
grantType ← configuration.getOptional[String]("auth.oauth2.grantType")
authorizationUrl ← configuration.getOptional[String]("auth.oauth2.authorizationUrl")
userUrl ← configuration.getOptional[String]("auth.oauth2.userUrl")
tokenUrl ← configuration.getOptional[String]("auth.oauth2.tokenUrl")
scope ← configuration.getOptional[String]("auth.oauth2.scope")
autocreate = configuration.getOptional[Boolean]("auth.sso.autocreate").getOrElse(false)
} yield OAuth2Config(clientId, clientSecret, redirectUri, responseType, grantType, authorizationUrl, tokenUrl, userUrl, scope, autocreate)
}
@Singleton
class OAuth2Srv(
ws: WSClient,
userSrv: UserSrv,
ssoMapper: UserMapper,
oauth2Config: Option[OAuth2Config],
implicit val ec: ExecutionContext,
implicit val mat: Materializer
) extends AuthSrv {
@Inject() def this(ws: WSClient, ssoMapper: UserMapper, userSrv: UserSrv, configuration: Configuration, ec: ExecutionContext, mat: Materializer) =
this(ws, userSrv, ssoMapper, OAuth2Config(configuration), ec, mat)
override val name: String = "oauth2"
private val logger = Logger(getClass)
val Oauth2TokenQueryString = "code"
private def withOAuth2Config[A](body: OAuth2Config ⇒ Future[A]): Future[A] =
oauth2Config.fold[Future[A]](Future.failed(AuthenticationError("OAuth2 not configured properly")))(body)
override def authenticate()(implicit request: RequestHeader): Future[AuthContext] =
withOAuth2Config { cfg ⇒
request
.queryString
.get(Oauth2TokenQueryString)
.flatMap(_.headOption)
.fold(createOauth2Redirect(cfg.clientId)) { code ⇒
getAuthTokenAndAuthenticate(cfg.clientId, code)
}
}
private def getAuthTokenAndAuthenticate(clientId: String, code: String)(implicit request: RequestHeader): Future[AuthContext] = {
logger.debug("Getting user token with the code from the response!")
withOAuth2Config { cfg ⇒
ws.url(cfg.tokenUrl)
.post(
Map(
"code" → code,
"grant_type" → cfg.grantType,
"client_secret" → cfg.clientSecret,
"redirect_uri" → cfg.redirectUri,
"client_id" → clientId
)
)
.recoverWith {
case error ⇒
logger.error(s"Token verification failure", error)
Future.failed(AuthenticationError("Token verification failure"))
}
.flatMap { r ⇒
r.status match {
case Status.OK ⇒
val accessToken = (r.json \ "access_token").asOpt[String].getOrElse("")
val authHeader = "Authorization" → s"bearer $accessToken"
ws.url(cfg.userUrl)
.addHttpHeaders(authHeader)
.get()
.flatMap { userResponse ⇒
if (userResponse.status != Status.OK) {
Future.failed(AuthenticationError(s"unexpected response from server: ${userResponse.status} ${userResponse.body}"))
} else {
val response = userResponse.json.asInstanceOf[JsObject]
getOrCreateUser(response, authHeader)
}
}
case _ ⇒
logger.error(s"unexpected response from server: ${r.status} ${r.body}")
Future.failed(AuthenticationError("unexpected response from server"))
}
}
}
}
private def getOrCreateUser(response: JsValue, authHeader: (String, String))(implicit request: RequestHeader): Future[AuthContext] =
withOAuth2Config { cfg ⇒
ssoMapper.getUserFields(response, Some(authHeader)).flatMap { userFields ⇒
val userId = userFields.getString("login").getOrElse("")
userSrv
.get(userId)
.flatMap(user ⇒ {
userSrv.getFromUser(request, user, name)
})
.recoverWith {
case authErr: AuthorizationError ⇒ Future.failed(authErr)
case _ if cfg.autocreate ⇒
userSrv.inInitAuthContext { implicit authContext ⇒
userSrv
.create(userFields)
.flatMap(user ⇒ {
userSrv.getFromUser(request, user, name)
})
}
}
}
}
private def createOauth2Redirect(clientId: String): Future[AuthContext] =
withOAuth2Config { cfg ⇒
val queryStringParams = Map[String, Seq[String]](
"scope" → Seq(cfg.scope),
"response_type" → Seq(cfg.responseType),
"redirect_uri" → Seq(cfg.redirectUri),
"client_id" → Seq(clientId)
)
Future.failed(OAuth2Redirect(cfg.authorizationUrl, queryStringParams))
}
}