fangpsh's blog

Authentik关联飞书登录

Authentik 的oauth2 source 没有飞书,不过可以使用通用的OpenID OAuth Source(Login using a Generic OpenID-Connect compliant provider.)达到类似的效果。

这个网友的issue 提到用github 的模板,感觉应该不太对,参考type/github.py的实现,get_base_user_properties() 方法里面username 取值不太对。但是这个网友issue 中那个”Profile url“很有用。我一开始用的配置是:

  • Authorize url: https://accounts.feishu.cn/open-apis/authen/v1/authorize
  • Token url: https://open.feishu.cn/open-apis/authen/v2/oauth/token
  • Profile url: https://open.feishu.cn/open-apis/authen/v1/user_info

创建feishu oauth2 source的时候,scope为* contact:user.email:readonly contact:user.employee_id:readonly,飞书开发后台对应应用记得申请权限。飞书的callback url 填oauth2 source创建之后的callback url。

发现登录时出错:”Authentication failed: Could not determine id.“,看了下代码, 这个错误在callback.py,get_user_id() 函数尝试在info 里面找id 这个key,但是实际上这个Profile url 的body 格式不太对(不标准),而且缺少了”id“,有网友说可以用dex进行转换。嫌弃麻烦。不过可以看到在types/oidc.py里面,get_user_id()重载成取”sub“了。恰好看到了这个老API符合标准,能用。

配置替换为:

  • Authorize url: https://accounts.feishu.cn/open-apis/authen/v1/authorize
  • Token url: https://open.feishu.cn/open-apis/authen/v2/oauth/token
  • Profile url: https://passport.feishu.cn/suite/passport/oauth/userinfo

还需要创建一个Property Mappings,例如:

return {
  "username": info.get("user_id"),
  "email": info.get("email"),
  "name": info.get("name"),
  "attributes": {
    "avatar": info.get("avatar_url"),
    "employee_no": info.get("employee_no"),  
  },
}

将这个property mapping 绑定到之前创建的飞书 OAuth2 Source。 将default-authentication-flow 这个flow的stage->default-authentication-identification ->source setting -> 增加feishu oauth2 source。 修改 default-source-enrollment ->stage->default-source-enrollment-write,创建内部账户,选择用户类型,用户组等等。

按照前面的mapping,拿到了飞书的头像,还需要在System->Setting->Avatar 修改为attributes.avatar,initials

大功告成。

可以搜到有一个网友提交了pr,增加了types/lark 的支持,但是一直没合并。所以拉到最新release,单独patch 下这个pr,build一下也可以。在这个pr 中我们也可以看到重载的get_user_id() 和get_base_user_properties ()等函数,可以按需修改。