This repository has been archived by the owner on Sep 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 142
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #104 from akkie/master
Add marker trait for AuthInfo
- Loading branch information
Showing
15 changed files
with
478 additions
and
21 deletions.
There are no files selected for viewing
43 changes: 43 additions & 0 deletions
43
app/com/mohiva/play/silhouette/contrib/daos/OAuth1InfoDAO.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/** | ||
* Copyright 2014 Mohiva Organisation (license at mohiva dot com) | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.mohiva.play.silhouette.contrib.daos | ||
|
||
import com.mohiva.play.silhouette.core.LoginInfo | ||
import com.mohiva.play.silhouette.core.providers.OAuth1Info | ||
import scala.concurrent.Future | ||
|
||
/** | ||
* The DAO to store the OAuth1 information. | ||
*/ | ||
trait OAuth1InfoDAO { | ||
|
||
/** | ||
* Saves the OAuth1 info. | ||
* | ||
* @param loginInfo The login info for which the auth info should be saved. | ||
* @param authInfo The OAuth1 info to save. | ||
* @return The saved OAuth1 info or None if the OAuth1 info couldn't be saved. | ||
*/ | ||
def save(loginInfo: LoginInfo, authInfo: OAuth1Info): Future[Option[OAuth1Info]] | ||
|
||
/** | ||
* Finds the OAuth1 info which is linked with the specified login info. | ||
* | ||
* @param loginInfo The linked login info. | ||
* @return The retrieved OAuth1 info or None if no OAuth1 info could be retrieved for the given login info. | ||
*/ | ||
def find(loginInfo: LoginInfo): Future[Option[OAuth1Info]] | ||
} |
43 changes: 43 additions & 0 deletions
43
app/com/mohiva/play/silhouette/contrib/daos/OAuth2InfoDAO.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/** | ||
* Copyright 2014 Mohiva Organisation (license at mohiva dot com) | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.mohiva.play.silhouette.contrib.daos | ||
|
||
import com.mohiva.play.silhouette.core.LoginInfo | ||
import com.mohiva.play.silhouette.core.providers.OAuth2Info | ||
import scala.concurrent.Future | ||
|
||
/** | ||
* The DAO to store the OAuth2 information. | ||
*/ | ||
trait OAuth2InfoDAO { | ||
|
||
/** | ||
* Saves the OAuth2 info. | ||
* | ||
* @param loginInfo The login info for which the auth info should be saved. | ||
* @param authInfo The OAuth2 info to save. | ||
* @return The saved OAuth2 info or None if the OAuth2 info couldn't be saved. | ||
*/ | ||
def save(loginInfo: LoginInfo, authInfo: OAuth2Info): Future[Option[OAuth2Info]] | ||
|
||
/** | ||
* Finds the OAuth2 info which is linked with the specified login info. | ||
* | ||
* @param loginInfo The linked login info. | ||
* @return The retrieved OAuth2 info or None if no OAuth2 info could be retrieved for the given login info. | ||
*/ | ||
def find(loginInfo: LoginInfo): Future[Option[OAuth2Info]] | ||
} |
43 changes: 43 additions & 0 deletions
43
app/com/mohiva/play/silhouette/contrib/daos/PasswordInfoDAO.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/** | ||
* Copyright 2014 Mohiva Organisation (license at mohiva dot com) | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.mohiva.play.silhouette.contrib.daos | ||
|
||
import com.mohiva.play.silhouette.core.LoginInfo | ||
import com.mohiva.play.silhouette.core.providers.PasswordInfo | ||
import scala.concurrent.Future | ||
|
||
/** | ||
* The DAO to store the password information. | ||
*/ | ||
trait PasswordInfoDAO { | ||
|
||
/** | ||
* Saves the password info. | ||
* | ||
* @param loginInfo The login info for which the auth info should be saved. | ||
* @param authInfo The password info to save. | ||
* @return The saved password info or None if the password info couldn't be saved. | ||
*/ | ||
def save(loginInfo: LoginInfo, authInfo: PasswordInfo): Future[Option[PasswordInfo]] | ||
|
||
/** | ||
* Finds the password info which is linked with the specified login info. | ||
* | ||
* @param loginInfo The linked login info. | ||
* @return The retrieved password info or None if no password info could be retrieved for the given login info. | ||
*/ | ||
def find(loginInfo: LoginInfo): Future[Option[PasswordInfo]] | ||
} |
92 changes: 92 additions & 0 deletions
92
app/com/mohiva/play/silhouette/contrib/services/DefaultAuthInfoService.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/** | ||
* Copyright 2014 Mohiva Organisation (license at mohiva dot com) | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package com.mohiva.play.silhouette.contrib.services | ||
|
||
import javax.inject.Inject | ||
import scala.reflect.ClassTag | ||
import scala.concurrent.Future | ||
import scala.concurrent.ExecutionContext.Implicits.global | ||
import com.mohiva.play.silhouette.core.LoginInfo | ||
import com.mohiva.play.silhouette.core.services.{ AuthInfo, AuthInfoService } | ||
import com.mohiva.play.silhouette.core.providers.{ OAuth2Info, OAuth1Info, PasswordInfo } | ||
import com.mohiva.play.silhouette.contrib.daos.{ OAuth2InfoDAO, OAuth1InfoDAO, PasswordInfoDAO } | ||
import DefaultAuthInfoService._ | ||
|
||
/** | ||
* An implementation of the auth info service which stores the different auth info instances with the help of | ||
* different DAOs. | ||
* | ||
* Due the nature of the different auth information it is hard to persist the data in a single data structure, | ||
* expect the data gets stored in a serialized format. With this implementation it is possible to store the | ||
* different auth info in different backing stores. If we speak of a relational database, then the auth info | ||
* can be stored in different tables. And the tables represents the internal data structure of each auth info | ||
* object. | ||
* | ||
* @param passwordInfoDAO The password info DAO implementation. | ||
* @param oauth1InfoDAO The OAuth1 info DAO implementation. | ||
* @param oauth2InfoDAO The OAuth2 info DAO implementation. | ||
*/ | ||
class DefaultAuthInfoService @Inject() ( | ||
passwordInfoDAO: PasswordInfoDAO, | ||
oauth1InfoDAO: OAuth1InfoDAO, | ||
oauth2InfoDAO: OAuth2InfoDAO) extends AuthInfoService { | ||
|
||
/** | ||
* Saves auth info. | ||
* | ||
* This method gets called when a user logs in(social auth) or registers. This is the change | ||
* to persist the auth info for a provider in the backing store. If the application supports | ||
* the concept of "merged identities", i.e., the same user being able to authenticate through | ||
* different providers, then make sure that the auth info for every linked login info gets | ||
* stored separately. | ||
* | ||
* @param loginInfo The login info for which the auth info should be saved. | ||
* @param authInfo The auth info to save. | ||
* @return The saved auth info or None if the auth info couldn't be saved. | ||
*/ | ||
def save[T <: AuthInfo](loginInfo: LoginInfo, authInfo: T): Future[Option[T]] = authInfo match { | ||
case a: PasswordInfo => passwordInfoDAO.save(loginInfo, a).map(_.map(_.asInstanceOf[T])) | ||
case a: OAuth1Info => oauth1InfoDAO.save(loginInfo, a).map(_.map(_.asInstanceOf[T])) | ||
case a: OAuth2Info => oauth2InfoDAO.save(loginInfo, a).map(_.map(_.asInstanceOf[T])) | ||
case a => throw new Exception(SaveError.format(a.getClass)) | ||
} | ||
|
||
/** | ||
* Retrieves the auth info which is linked with the specified login info. | ||
* | ||
* @param loginInfo The linked login info. | ||
* @param tag The class tag of the auth info. | ||
* @return The retrieved auth info or None if no auth info could be retrieved for the given login info. | ||
*/ | ||
def retrieve[T <: AuthInfo](loginInfo: LoginInfo)(implicit tag: ClassTag[T]): Future[Option[T]] = tag match { | ||
case a if a.runtimeClass == classOf[PasswordInfo] => passwordInfoDAO.find(loginInfo).map(_.map(_.asInstanceOf[T])) | ||
case a if a.runtimeClass == classOf[OAuth1Info] => oauth1InfoDAO.find(loginInfo).map(_.map(_.asInstanceOf[T])) | ||
case a if a.runtimeClass == classOf[OAuth2Info] => oauth2InfoDAO.find(loginInfo).map(_.map(_.asInstanceOf[T])) | ||
case a => throw new Exception(RetrieveError.format(a.runtimeClass)) | ||
} | ||
} | ||
|
||
/** | ||
* The companion object. | ||
*/ | ||
object DefaultAuthInfoService { | ||
|
||
/** | ||
* The error messages. | ||
*/ | ||
val SaveError = "Cannot save auth info of type: %s" | ||
val RetrieveError = "Cannot search for auth info of type: %s" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.