Skip to content

Commit

Permalink
Improve Google SAML support
Browse files Browse the repository at this point in the history
Fix CSS
  • Loading branch information
vincelwt committed May 20, 2024
1 parent aebfc31 commit c26091f
Showing 1 changed file with 22 additions and 9 deletions.
31 changes: 22 additions & 9 deletions packages/backend/src/api/v1/auth/saml.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,22 +76,35 @@ export async function getLoginUrl(orgId: string) {
return context
}

function parseAttributes(attributes: any) {
let email = ""
// This function parses the attributes from the SAML response
// and returns the email and name
function parseAttributes(attributes: any, nameID: string) {
let email = nameID
let name = ""

for (const key in attributes) {
if (key.toLowerCase().includes("emailaddress")) {
if (
key.toLowerCase().includes("emailaddress") ||
key.toLowerCase() === "email"
) {
email = sanitizeEmail(attributes[key])
} else if (key.toLowerCase().includes("displayname")) {
} else if (
key.toLowerCase().includes("displayname") ||
key.toLowerCase() === "name"
) {
name = attributes[key]
}
}

if (!name && attributes.firstname && attributes.lastname) {
name = `${attributes.firstname} ${attributes.lastname}`
}

return { email, name }
}

route.get("/success", async (ctx: Context) => {
const { orgId } = ctx.params as { orgId: string }
// const { orgId } = ctx.params as { orgId: string }

ctx.redirect(process.env.APP_URL!)
})
Expand Down Expand Up @@ -137,7 +150,7 @@ route.post("/acs", async (ctx: Context) => {

const parsedResult = await sp.parseLoginResponse(idp, "post", ctx.request)

const { attributes, conditions } = parsedResult.extract
const { attributes, conditions, nameID } = parsedResult.extract

if (!attributes) {
ctx.throw(400, "No attributes found")
Expand All @@ -153,7 +166,7 @@ route.post("/acs", async (ctx: Context) => {
}
}

const { email, name } = parseAttributes(attributes)
const { email, name } = parseAttributes(attributes, nameID)

const singleUseToken = await generateOneTimeToken()

Expand All @@ -172,8 +185,8 @@ route.post("/acs", async (ctx: Context) => {
})

route.post("/slo", async (ctx: Context) => {
const { orgId } = ctx.params as { orgId: string }
ctx.body = "SAML SLO received for orgId: " + orgId
// const { orgId } = ctx.params as { orgId: string }
ctx.body = "SAML SLO"
})

export default route

0 comments on commit c26091f

Please sign in to comment.