File tree Expand file tree Collapse file tree
lib/cadet/auth/providers/openid Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ defmodule Cadet.Auth.Providers.NusEntraIdClaimExtractor do
2+ @ moduledoc """
3+ Extracts fields from NUS' Microsoft Entra ID JWTs.
4+ """
5+
6+ @ behaviour Cadet.Auth.Providers.OpenID.ClaimExtractor
7+
8+ def get_username ( claims , access_token ) do
9+ get_userinfo ( claims , "samAccountName" )
10+ end
11+
12+ def get_name ( claims , access_token ) do
13+ get_userinfo ( claims , "displayName" )
14+ end
15+
16+ def get_token_type , do: "id_token"
17+
18+ # Allowed Active Directory (AD) domains; modify as needed
19+ @ allowed_domains ~w( student alum staff)
20+
21+ defp check_allowed_domain ( claims ) do
22+ domain = Map . get ( claims , "ExtensionAttribute6" )
23+ domain in @ allowed_domains
24+ end
25+
26+ defp map_key_to_raw ( key ) , do: key
27+
28+ defp get_userinfo ( claims , key ) do
29+ with true <- check_allowed_domain ( claims ) ,
30+ mapped_key <- map_key_to_raw ( key ) ,
31+ value when not is_nil ( value ) <- Map . get ( claims , mapped_key ) do
32+ value
33+ else
34+ false -> nil
35+ _ -> nil
36+ end
37+ end
38+ end
You can’t perform that action at this time.
0 commit comments