forked from jaliss/securesocial
-
Notifications
You must be signed in to change notification settings - Fork 1
/
User.scala
251 lines (206 loc) · 7.19 KB
/
User.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
package models
import play.api.db.slick.Config.driver.simple._
import play.api.libs.json._
import securesocial.core._
import play.api.libs.Codecs
import scala.language.implicitConversions
import scala.language.postfixOps
case class User(
id: Option[Long],
identityId: IdentityId,
firstName: String,
lastName: String,
email: Option[String],
avatarUrl: Option[String],
authMethod: AuthenticationMethod,
oAuth1Info: Option[OAuth1Info],
oAuth2Info: Option[OAuth2Info],
passwordInfo: Option[PasswordInfo]
) extends Identity {
def fullName: String = s"$firstName $lastName"
def avatar: Option[String] = avatarUrl.orElse {
email.map { e => s"http://www.gravatar.com/avatar/${Codecs.md5(e.getBytes)}.png" }
}
def updateIdentity(i: Identity): User = {
this.copy(
identityId = i.identityId,
firstName = i.firstName,
lastName = i.lastName,
email = i.email,
authMethod = i.authMethod,
avatarUrl = i.avatarUrl,
oAuth1Info = i.oAuth1Info,
oAuth2Info = i.oAuth2Info,
passwordInfo = i.passwordInfo
)
}
}
object User {
def apply(i: Identity): User = {
new User(
id = None,
identityId = i.identityId,
firstName = i.firstName,
lastName = i.lastName,
email = i.email,
authMethod = i.authMethod,
avatarUrl = i.avatarUrl,
oAuth1Info = i.oAuth1Info,
oAuth2Info = i.oAuth2Info,
passwordInfo = i.passwordInfo
)
}
implicit val jsonWrites = new Writes[User] {
def writes(o: User): JsValue = {
Json.obj(
"email" -> o.email
)
}
}
}
class UserTable(tag: Tag) extends Table[User](tag, "users") {
implicit val AuthenticationMethodColumnType = MappedColumnType.base[AuthenticationMethod, String](_.method, AuthenticationMethod(_))
def id = column[Long]("id", O.PrimaryKey, O.AutoInc)
def userId = column[String]("userId")
def providerId = column[String]("providerId")
def firstName = column[String]("firstName")
def lastName = column[String]("lastName")
def email = column[Option[String]]("email")
def avatarUrl = column[Option[String]]("avatarUrl")
def authMethod = column[AuthenticationMethod]("authMethod")
def oAuth1InfoToken = column[Option[String]]("oAuth1InfoToken")
def oAuth1InfoSecret = column[Option[String]]("oAuth1InfoSecret")
def oAuth2InfoAccessToken = column[Option[String]]("oAuth1InfoAccessToken")
def oAuth2InfoTokenType = column[Option[String]]("oAuth1InfoTokenType")
def oAuth2InfoExpiresIn = column[Option[Int]]("oAuth1InfoExpiresIn")
def oAuth2InfoRefreshToken = column[Option[String]]("oAuth1InfoRefreshToken")
def passwordInfoHasher = column[Option[String]]("passwordInfoHasher")
def passwordInfoPassword = column[Option[String]]("passwordInfoPassword")
def passwordInfoSalt = column[Option[String]]("passwordInfoSalt")
def userIdProviderIdIndex = index("userprovider_index", (userId, providerId), unique = true)
def emailIndex = index("email_index", email, unique = true)
implicit def tuple2IdentityId(tuple: (String, String)) = tuple match {
case (userId, providerId) => IdentityId(userId, providerId)
}
implicit def tuple2OAuth1Info(tuple: (Option[String], Option[String])) = tuple match {
case (Some(token), Some(secret)) => Some(OAuth1Info(token, secret))
case _ => None
}
implicit def tuple2OAuth2Info(tuple: (Option[String], Option[String], Option[Int], Option[String])) = tuple match {
case (Some(token), tokenType, expiresIn, refreshToken) => Some(OAuth2Info(token, tokenType, expiresIn, refreshToken))
case _ => None
}
implicit def tuple2PasswordInfo(tuple: (Option[String], Option[String], Option[String])) = tuple match {
case (Some(hasher), Some(password), salt) => Some(PasswordInfo(hasher, password, salt))
case _ => None
}
private def columnsToUser(
id: Option[Long],
userId: String,
providerId: String,
firstName: String,
lastName: String,
email: Option[String],
avatarUrl: Option[String],
authMethod: AuthenticationMethod,
oAuth1InfoToken: Option[String],
oAuth1InfoSecret: Option[String],
oAuth2InfoAccessToken: Option[String],
oAuth2InfoTokenType: Option[String],
oAuth2InfoExpiresIn: Option[Int],
oAuth2InfoRefreshToken: Option[String],
passwordInfoHasher: Option[String],
passwordInfoPassword: Option[String],
passwordInfoSalt: Option[String]
): User = {
User(
id,
(userId, providerId),
firstName,
lastName,
email,
avatarUrl,
authMethod,
(oAuth1InfoToken, oAuth1InfoSecret),
(oAuth2InfoAccessToken, oAuth2InfoTokenType, oAuth2InfoExpiresIn, oAuth2InfoRefreshToken),
(passwordInfoHasher, passwordInfoPassword, passwordInfoSalt)
)
}
private def userToColumns(u: User) = {
Some((
u.id,
u.identityId.userId,
u.identityId.providerId,
u.firstName,
u.lastName,
u.email,
u.avatarUrl,
u.authMethod,
u.oAuth1Info.map(_.token),
u.oAuth1Info.map(_.secret),
u.oAuth2Info.map(_.accessToken),
u.oAuth2Info.flatMap(_.tokenType),
u.oAuth2Info.flatMap(_.expiresIn),
u.oAuth2Info.flatMap(_.refreshToken),
u.passwordInfo.map(_.hasher),
u.passwordInfo.map(_.password),
u.passwordInfo.flatMap(_.salt)
))
}
def * = (
id.?,
userId,
providerId,
firstName,
lastName,
email,
avatarUrl,
authMethod,
oAuth1InfoToken,
oAuth1InfoSecret,
oAuth2InfoAccessToken,
oAuth2InfoTokenType,
oAuth2InfoExpiresIn,
oAuth2InfoRefreshToken,
passwordInfoHasher,
passwordInfoPassword,
passwordInfoSalt
) <> (columnsToUser _ tupled, userToColumns)
}
object Users extends TableQuery(new UserTable(_)) {
// Queries
val queryByUserIdAndProviderId = Compiled((userId: Column[String], providerId: Column[String]) =>
Users.filter(u => u.userId === userId && u.providerId === providerId)
)
val queryByEmailAndProviderId = Compiled((email: Column[String], providerId: Column[String]) => {
Users.filter(u => u.email === email && u.providerId === providerId)
})
val queryByEmail = Compiled((email: Column[String]) => {
Users.filter(u => u.email === email)
})
// Finders
def findByIdentityId(identityId: IdentityId)(implicit session: Session): Option[User] = {
Users.queryByUserIdAndProviderId(identityId.userId, identityId.providerId).firstOption
}
def findByEmailAndProviderId(email: String, providerId: String)(implicit session: Session): Option[User] = {
queryByEmailAndProviderId(email, providerId).firstOption
}
def findByEmail(email: String)(implicit session: Session): Option[User] = {
queryByEmail(email).firstOption
}
def save(i: Identity)(implicit session: Session): Identity = {
// If the identity has an email use that, otherwise fallback to userid and providerid
val query = i.email.map(queryByEmail(_)).getOrElse(queryByUserIdAndProviderId(i.identityId.userId, i.identityId.providerId))
query.firstOption match {
case Some(user) => {
query.update(user.updateIdentity(i))
i
}
case None => {
val user = User(i)
val id = (Users returning Users.map(_.id)) += User(i)
user.copy(id = Some(id))
}
}
}
}