Skip to content
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

Basic implementation of OpenId Connect groups #2202

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
44 changes: 43 additions & 1 deletion src/main/scala/gitbucket/core/service/AccountService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,49 @@ trait AccountService {
List(userName) ++
Collaborators.filter(_.collaboratorName === userName.bind).sortBy(_.userName).map(_.userName).list.distinct
}

def isUserGroupMember(
groupName: String,
userName: String
)(implicit s: Session) : List[Boolean] = {
GroupMembers
.filter(_.userName === userName.bind)
.filter(_.groupName === groupName.bind)
.map(_.isManager)
.list
}
def removeUserFromGroup(
groupName: String,
userName: String
)(implicit s: Session) : Unit = {
GroupMembers
.filter(_.userName === userName.bind)
.filter(_.groupName === groupName.bind)
.delete
}
def addUserToGroup(
groupName: String,
userName: String,
isManager: Boolean
)(implicit s: Session): Unit = {
GroupMembers
.filter(_.userName === userName.bind)
.filter(_.groupName === groupName.bind)
.delete
GroupMembers insert GroupMember(groupName, userName, isManager)
}
def isGroupAccount(groupName: String)(implicit s: Session): Boolean = {
getAccountByUserName(groupName).collect {
case account if !account.isGroupAccount =>
//~ logger.info(s"Account: is ${account}) (UserAcct)")
false
case account if account.isGroupAccount =>
//~ logger.info(s"Account: is ${account} (groupAcct)")
return true
case _ => false
}
//~ logger.info(s"Not found! ${groupName}")
return false
}
}

object AccountService extends AccountService
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import com.nimbusds.openid.connect.sdk.claims.IDTokenClaimsSet
import com.nimbusds.openid.connect.sdk.op.OIDCProviderMetadata
import com.nimbusds.openid.connect.sdk.validators.IDTokenValidator
import com.nimbusds.openid.connect.sdk.{AuthenticationErrorResponse, _}
import net.minidev.json.JSONArray
import gitbucket.core.model.Account
import gitbucket.core.model.Profile.profile.blockingApi._
import org.slf4j.LoggerFactory
Expand Down Expand Up @@ -52,6 +53,78 @@ trait OpenIDConnectService {
)
}

def syncOIDCremoveGroups(
userName: String,
groups: JSONArray,
trygrp: String
)(implicit s: Session) : Unit = {
var isManager = AccountService.isUserGroupMember(trygrp,userName)
//~ logger.info(s"user: ${userName} grp: ${trygrp} isManage: ${isManager} ${isManager.getClass.getName}")
if (isManager.size == 0) {
return
}
var oidc_grp = if (isManager(0)) {
s"${trygrp}-admin"
} else {
s"${trygrp}-user"
}
//~ logger.info(s"trygrp=${trygrp} oidc_grp=${oidc_grp}")
if (!groups.contains(oidc_grp)) {
logger.info(s"Removing ${userName} from ${trygrp} group")
AccountService.removeUserFromGroup(trygrp,userName)
}
}
def syncOIDCaddGroups(
userName: String,
groupName: String
)(implicit s: Session) : Unit = {
//~ logger.info(s"GROUP(${groupName}): ${userName}")
var gbGrpName = ""
var gbIsManager = false
if (groupName.endsWith("-user")) {
gbGrpName = groupName.substring(0,groupName.length()-5)
gbIsManager = false
} else if (groupName.endsWith("-admin")) {
gbGrpName = groupName.substring(0,groupName.length()-6)
gbIsManager = true
} else {
/* Do nothing! */
return
}
//~ logger.info(s"gbGrpName: ${gbGrpName} admin: ${gbIsManager}")
if (!AccountService.isGroupAccount(gbGrpName)) {
return
}
var isMember = AccountService.isUserGroupMember(gbGrpName,userName)
if (isMember.size != 0) {
/* Already member... */
return
}
logger.info(s"Adding ${userName} to ${gbGrpName} (Manager: ${gbIsManager}")
AccountService.addUserToGroup(gbGrpName,userName,gbIsManager)
}
/**
* If "groups" claim is found, we synchronize groups.
*
* @param userName User being synchronized
* @param groups List of groups being claimed
*/
def syncOIDCgroups(
userName: String,
groups: JSONArray
)(implicit s: Session) : Unit = {
/* First process member removals... */
var cgrps = AccountService.getGroupsByUserName(userName)
for (i <- 0 until cgrps.length) {
//~ logger.info(s"i=${i} grp: ${cgrps(i)}")
syncOIDCremoveGroups(userName, groups, cgrps(i))
}
/* Now process member additions... */
for (i <- 0 until groups.size) {
syncOIDCaddGroups(userName, groups.get(i).asInstanceOf[String])
}
}

/**
* Proceed the OpenID Connect authentication.
*
Expand All @@ -73,13 +146,18 @@ trait OpenIDConnectService {
obtainOIDCToken(authenticationResponse.getAuthorizationCode, nonce, redirectURI, oidc) flatMap { claims =>
Seq("email", "preferred_username", "name").map(k => Option(claims.getStringClaim(k))) match {
case Seq(Some(email), preferredUsername, name) =>
getOrCreateFederatedUser(
val res = getOrCreateFederatedUser(
claims.getIssuer.getValue,
claims.getSubject.getValue,
email,
preferredUsername,
name
)
if (claims.getClaim("groups") != null) {
logger.info(s"Sync groups for ${res.get.userName}: ${claims.getClaim("groups")}")
syncOIDCgroups(res.get.userName, claims.getClaim("groups").asInstanceOf[JSONArray])
}
return res
case _ =>
logger.info(s"OIDC ID token must have an email claim: claims=${claims.toJSONObject}")
None
Expand Down