|
| 1 | +package services |
| 2 | + |
| 3 | +import play.api.libs.ws.WS |
| 4 | +import play.api.{Application, Logger} |
| 5 | +import play.api.libs.json.JsObject |
| 6 | +import securesocial.core._ |
| 7 | +import scala.collection.JavaConverters._ |
| 8 | + |
| 9 | + |
| 10 | +/** |
| 11 | + * A Keycloak OAuth2 Provider |
| 12 | + */ |
| 13 | +class KeycloakProvider(application: Application) extends OAuth2Provider(application) { |
| 14 | + val Error = "error" |
| 15 | + val Message = "message" |
| 16 | + val Type = "type" |
| 17 | + val Sub = "sub" |
| 18 | + val Name = "name" |
| 19 | + val GivenName = "given_name" |
| 20 | + val FamilyName = "family_name" |
| 21 | + // todo: picture wont work |
| 22 | + val Picture = "picture" |
| 23 | + val Email = "email" |
| 24 | + val Groups = "groups" |
| 25 | + |
| 26 | + override def id = KeycloakProvider.Keycloak |
| 27 | + |
| 28 | + def fillProfile(user: SocialUser): SocialUser = { |
| 29 | + val UserInfoApi = loadProperty("userinfoUrl").getOrElse(throwMissingPropertiesException()) |
| 30 | + val accessToken = user.oAuth2Info.get.accessToken |
| 31 | + val promise = WS.url(UserInfoApi.toString).withHeaders(("Authorization", "Bearer " + accessToken)).get() |
| 32 | + |
| 33 | + try { |
| 34 | + val response = awaitResult(promise) |
| 35 | + val me = response.json |
| 36 | + Logger.debug("Got back from Keycloak : " + me.toString()) |
| 37 | + (me \ Error).asOpt[JsObject] match { |
| 38 | + case Some(error) => |
| 39 | + val message = (error \ Message).as[String] |
| 40 | + val errorType = ( error \ Type).as[String] |
| 41 | + Logger.error("[securesocial] error retrieving profile information from Keycloak. Error type = %s, message = %s" |
| 42 | + .format(errorType,message)) |
| 43 | + throw new AuthenticationException() |
| 44 | + case _ => |
| 45 | + val userId = (me \ Sub).as[String] |
| 46 | + val firstName = (me \ GivenName).asOpt[String] |
| 47 | + val lastName = (me \ FamilyName).asOpt[String] |
| 48 | + val fullName = (me \ Name).asOpt[String] |
| 49 | + val avatarUrl = ( me \ Picture).asOpt[String] |
| 50 | + val email = ( me \ Email).asOpt[String] |
| 51 | + val groups = ( me \ Groups).asOpt[List[String]] |
| 52 | + val roles = ( me \ "resource_access" \ "account" \ "roles").asOpt[List[String]] |
| 53 | + (application.configuration.getList("securesocial.keycloak.groups"), groups) match { |
| 54 | + case (Some(conf), Some(keycloak)) => { |
| 55 | + val conflist = conf.unwrapped().asScala.toList |
| 56 | + if (keycloak.intersect(conflist).isEmpty) { |
| 57 | + throw new AuthenticationException() |
| 58 | + } |
| 59 | + } |
| 60 | + case (Some(_), None) => throw new AuthenticationException() |
| 61 | + case (None, _) => Logger.debug("[securesocial] No check needed for groups") |
| 62 | + } |
| 63 | + (application.configuration.getList("securesocial.keycloak.roles"), roles) match { |
| 64 | + case (Some(conf), Some(keycloak)) => { |
| 65 | + val conflist = conf.unwrapped().asScala.toList |
| 66 | + if (keycloak.intersect(conflist).isEmpty) { |
| 67 | + throw new AuthenticationException() |
| 68 | + } |
| 69 | + } |
| 70 | + case (Some(_), None) => throw new AuthenticationException() |
| 71 | + case (None, _) => Logger.debug("[securesocial] No check needed for roles") |
| 72 | + } |
| 73 | + user.copy( |
| 74 | + identityId = IdentityId(userId, id), |
| 75 | + firstName = firstName.getOrElse(""), |
| 76 | + lastName = lastName.getOrElse(""), |
| 77 | + fullName = fullName.getOrElse({ |
| 78 | + if (firstName.isDefined && lastName.isDefined) { |
| 79 | + firstName.get + " " + lastName.get |
| 80 | + } else if (firstName.isDefined) { |
| 81 | + firstName.get |
| 82 | + } else if (lastName.isDefined) { |
| 83 | + lastName.get |
| 84 | + } else { |
| 85 | + "" |
| 86 | + } |
| 87 | + }), |
| 88 | + avatarUrl = avatarUrl, |
| 89 | + email = email |
| 90 | + ) |
| 91 | + } |
| 92 | + } catch { |
| 93 | + case e: Exception => { |
| 94 | + Logger.error( "[securesocial] error retrieving profile information from Keycloak", e) |
| 95 | + throw new AuthenticationException() |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +object KeycloakProvider { |
| 102 | + val Keycloak = "keycloak" |
| 103 | +} |
0 commit comments