-
Notifications
You must be signed in to change notification settings - Fork 19
419 authenticator against keycloak #420
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
2159033
follow cilogon example
longshuicy 8d2bcd6
add keycloak
longshuicy c1d5302
keycloak is working now
longshuicy 0e2e732
changelog
longshuicy 8faf7dc
revert accidental change
longshuicy f9b352b
replace the default conf to clowder
longshuicy dd2c7ae
typo
longshuicy f686690
use the correct group
longshuicy 388496c
check roles also
longshuicy ae950fd
provide examples of filtering by grouops and roles
longshuicy 8d6c34c
Merge branch 'develop' into 419-authenticator-against-keycloak
lmarini File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,103 @@ | ||
| package services | ||
|
|
||
| import play.api.libs.ws.WS | ||
| import play.api.{Application, Logger} | ||
| import play.api.libs.json.JsObject | ||
| import securesocial.core._ | ||
| import scala.collection.JavaConverters._ | ||
|
|
||
|
|
||
| /** | ||
| * A Keycloak OAuth2 Provider | ||
| */ | ||
| class KeycloakProvider(application: Application) extends OAuth2Provider(application) { | ||
| val Error = "error" | ||
| val Message = "message" | ||
| val Type = "type" | ||
| val Sub = "sub" | ||
| val Name = "name" | ||
| val GivenName = "given_name" | ||
| val FamilyName = "family_name" | ||
| // todo: picture wont work | ||
| val Picture = "picture" | ||
| val Email = "email" | ||
| val Groups = "groups" | ||
|
|
||
| override def id = KeycloakProvider.Keycloak | ||
|
|
||
| def fillProfile(user: SocialUser): SocialUser = { | ||
| val UserInfoApi = loadProperty("userinfoUrl").getOrElse(throwMissingPropertiesException()) | ||
| val accessToken = user.oAuth2Info.get.accessToken | ||
| val promise = WS.url(UserInfoApi.toString).withHeaders(("Authorization", "Bearer " + accessToken)).get() | ||
|
|
||
| try { | ||
| val response = awaitResult(promise) | ||
| val me = response.json | ||
| Logger.debug("Got back from Keycloak : " + me.toString()) | ||
| (me \ Error).asOpt[JsObject] match { | ||
| case Some(error) => | ||
| val message = (error \ Message).as[String] | ||
| val errorType = ( error \ Type).as[String] | ||
| Logger.error("[securesocial] error retrieving profile information from Keycloak. Error type = %s, message = %s" | ||
| .format(errorType,message)) | ||
| throw new AuthenticationException() | ||
| case _ => | ||
| val userId = (me \ Sub).as[String] | ||
| val firstName = (me \ GivenName).asOpt[String] | ||
| val lastName = (me \ FamilyName).asOpt[String] | ||
| val fullName = (me \ Name).asOpt[String] | ||
| val avatarUrl = ( me \ Picture).asOpt[String] | ||
| val email = ( me \ Email).asOpt[String] | ||
| val groups = ( me \ Groups).asOpt[List[String]] | ||
| val roles = ( me \ "resource_access" \ "account" \ "roles").asOpt[List[String]] | ||
| (application.configuration.getList("securesocial.keycloak.groups"), groups) match { | ||
| case (Some(conf), Some(keycloak)) => { | ||
| val conflist = conf.unwrapped().asScala.toList | ||
| if (keycloak.intersect(conflist).isEmpty) { | ||
| throw new AuthenticationException() | ||
| } | ||
| } | ||
| case (Some(_), None) => throw new AuthenticationException() | ||
| case (None, _) => Logger.debug("[securesocial] No check needed for groups") | ||
| } | ||
| (application.configuration.getList("securesocial.keycloak.roles"), roles) match { | ||
| case (Some(conf), Some(keycloak)) => { | ||
| val conflist = conf.unwrapped().asScala.toList | ||
| if (keycloak.intersect(conflist).isEmpty) { | ||
| throw new AuthenticationException() | ||
| } | ||
| } | ||
| case (Some(_), None) => throw new AuthenticationException() | ||
| case (None, _) => Logger.debug("[securesocial] No check needed for roles") | ||
| } | ||
| user.copy( | ||
| identityId = IdentityId(userId, id), | ||
| firstName = firstName.getOrElse(""), | ||
| lastName = lastName.getOrElse(""), | ||
| fullName = fullName.getOrElse({ | ||
| if (firstName.isDefined && lastName.isDefined) { | ||
| firstName.get + " " + lastName.get | ||
| } else if (firstName.isDefined) { | ||
| firstName.get | ||
| } else if (lastName.isDefined) { | ||
| lastName.get | ||
| } else { | ||
| "" | ||
| } | ||
| }), | ||
| avatarUrl = avatarUrl, | ||
| email = email | ||
| ) | ||
| } | ||
| } catch { | ||
| case e: Exception => { | ||
| Logger.error( "[securesocial] error retrieving profile information from Keycloak", e) | ||
| throw new AuthenticationException() | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| object KeycloakProvider { | ||
| val Keycloak = "keycloak" | ||
| } | ||
This file contains hidden or 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 hidden or 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.