-
Notifications
You must be signed in to change notification settings - Fork 17
/
OAuthController.scala
163 lines (140 loc) · 5.8 KB
/
OAuthController.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
155
156
157
158
159
160
161
162
163
package controllers
import models._
import play.api.libs.json.{Json, Writes}
import play.api.mvc.{Action, Controller}
import scalikejdbc.DB
import scala.concurrent.Future
import scalaoauth2.provider._
import scalaoauth2.provider.OAuth2ProviderActionBuilders._
class OAuthController extends Controller with OAuth2Provider {
implicit val authInfoWrites = new Writes[AuthInfo[Account]] {
def writes(authInfo: AuthInfo[Account]) = {
Json.obj(
"account" -> Json.obj(
"email" -> authInfo.user.email
),
"clientId" -> authInfo.clientId,
"redirectUri" -> authInfo.redirectUri
)
}
}
override val tokenEndpoint = new TokenEndpoint {
override val handlers = Map(
OAuthGrantType.AUTHORIZATION_CODE -> new AuthorizationCode(),
OAuthGrantType.REFRESH_TOKEN -> new RefreshToken(),
OAuthGrantType.CLIENT_CREDENTIALS -> new ClientCredentials(),
OAuthGrantType.PASSWORD -> new Password()
)
}
def accessToken = Action.async { implicit request =>
issueAccessToken(new MyDataHandler())
}
def resources = AuthorizedAction(new MyDataHandler()) { request =>
Ok(Json.toJson(request.authInfo))
}
class MyDataHandler extends DataHandler[Account] {
// common
override def validateClient(maybeCredential: Option[ClientCredential], request: AuthorizationRequest): Future[Boolean] = DB.readOnly { implicit session =>
Future.successful((for {
clientCredential <- maybeCredential
} yield OauthClient.validate(clientCredential.clientId, clientCredential.clientSecret.getOrElse(""), request.grantType)).contains(true))
}
override def getStoredAccessToken(authInfo: AuthInfo[Account]): Future[Option[AccessToken]] = DB.readOnly { implicit session =>
Future.successful(OauthAccessToken.findByAuthorized(authInfo.user, authInfo.clientId.getOrElse("")).map(toAccessToken))
}
override def createAccessToken(authInfo: AuthInfo[Account]): Future[AccessToken] = DB.localTx { implicit session =>
val clientId = authInfo.clientId.getOrElse(throw new InvalidClient())
val oauthClient = OauthClient.findByClientId(clientId).getOrElse(throw new InvalidClient())
val accessToken = OauthAccessToken.create(authInfo.user, oauthClient)
Future.successful(toAccessToken(accessToken))
}
private val accessTokenExpireSeconds = 3600
private def toAccessToken(accessToken: OauthAccessToken) = {
AccessToken(
accessToken.accessToken,
Some(accessToken.refreshToken),
None,
Some(accessTokenExpireSeconds),
accessToken.createdAt.toDate
)
}
override def findUser(maybeCredential: Option[ClientCredential], request: AuthorizationRequest): Future[Option[Account]] = DB.readOnly { implicit session =>
request match {
case request: PasswordRequest =>
Future.successful(Account.authenticate(request.username, request.password))
case request: ClientCredentialsRequest =>
Future.successful {
for {
clientCredential <- maybeCredential
account <- OauthClient.findClientCredentials(
clientCredential.clientId,
clientCredential.clientSecret.getOrElse("")
)
} yield account
}
case _ =>
Future.successful(None)
}
}
// Refresh token grant
override def findAuthInfoByRefreshToken(refreshToken: String): Future[Option[AuthInfo[Account]]] = DB.readOnly { implicit session =>
Future.successful(OauthAccessToken.findByRefreshToken(refreshToken).flatMap { accessToken =>
for {
account <- accessToken.account
client <- accessToken.oauthClient
} yield {
AuthInfo(
user = account,
clientId = Some(client.clientId),
scope = None,
redirectUri = None
)
}
})
}
override def refreshAccessToken(authInfo: AuthInfo[Account], refreshToken: String): Future[AccessToken] = DB.localTx { implicit session =>
val clientId = authInfo.clientId.getOrElse(throw new InvalidClient())
val client = OauthClient.findByClientId(clientId).getOrElse(throw new InvalidClient())
val accessToken = OauthAccessToken.refresh(authInfo.user, client)
Future.successful(toAccessToken(accessToken))
}
// Authorization code grant
override def findAuthInfoByCode(code: String): Future[Option[AuthInfo[Account]]] = DB.readOnly { implicit session =>
Future.successful(OauthAuthorizationCode.findByCode(code).flatMap { authorization =>
for {
account <- authorization.account
client <- authorization.oauthClient
} yield {
AuthInfo(
user = account,
clientId = Some(client.clientId),
scope = None,
redirectUri = authorization.redirectUri
)
}
})
}
override def deleteAuthCode(code: String): Future[Unit] = DB.localTx { implicit session =>
Future.successful(OauthAuthorizationCode.delete(code))
}
// Protected resource
override def findAccessToken(token: String): Future[Option[AccessToken]] = DB.readOnly { implicit session =>
Future.successful(OauthAccessToken.findByAccessToken(token).map(toAccessToken))
}
override def findAuthInfoByAccessToken(accessToken: AccessToken): Future[Option[AuthInfo[Account]]] = DB.readOnly { implicit session =>
Future.successful(OauthAccessToken.findByAccessToken(accessToken.token).flatMap { case accessToken =>
for {
account <- accessToken.account
client <- accessToken.oauthClient
} yield {
AuthInfo(
user = account,
clientId = Some(client.clientId),
scope = None,
redirectUri = None
)
}
})
}
}
}