dsjq6977 2017-11-03 17:56
浏览 108
已采纳

从Go中的SAML响应中获取用户名

I am trying to build a Go-based webserver (running in Azure) that allows for single sign-on using SAML. Part of the criteria for the application is that there are two layers of access: first it should be decided whether a user has access to the webpage itself, and second the user should only be able to access the data that he is entitled to see.

I have looked at the listed libraries listed at godoc, but I cannot seem to find a way to implement the second criterion. We want to use the username/ID associated with the SAML response as a part of the database query. I cannot seem to find though where I can find this information. At the moment it seems like I should do something like

http.Handle("/apicall", samlSP.RequireAccount(http.HandlerFunc(foo)))

func foo(w http.ResponseWriter, r *http.Request) {
    user := // ?
    body, err := ioutil.ReadAll(r.Body)
    if err != nil {
        log.Println(err)
    }
    var filter FilterParameters
    err = json.Unmarshal(body, &filter)
    if err != nil {
        log.Println(err)
    }
    apiStruct := API(filter, user)
    json.NewEncoder(w).Encode(apiStruct)
}

However, I am not sure how to get the variable 'user' filled in the correct scope, and where I can get this information from. I was looking to use github.com/crewjam/saml, but I am flexible in switching to a different solution. The godoc mentions a pointer in the 'options' struct to a 'saml.EntityDescriptor' struct, which seems to contain a field for a username, but I am not sure if this would work, and how to even access this in the scope of my function "foo".

  • 写回答

1条回答 默认 最新

  • doubei8168 2017-11-06 13:03
    关注

    A "username" in SAML can come from the IdP in any form. It is contained in an attribute which you either know beforehand or you ask the IdP maintainer to release for you. Have a look at the SAML Response here

    In the response there is an AttributeStatement containing multiple Attribute assertions. One of those Attribute assertions is uid which is interpreted as meaning "username". In this case the "username" is "test".

    EntityDescriptor is part of the SAML2 Metadata specification and is not used to hold attributes. It's used to describe entities such as SP and IdP so they can trust each other.

    For deciding which parts of an application a user should get access to you can use the eduPersonEntitlement Attribute. This usually contains a URI or URN such as https://your.app.com/entitlment/admin

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀