mod_oidc_userinfo_vcard4/mod_oidc_userinfo_vcard4.lua
changeset 5354 f8ec43db580b
child 5364 f05de5ac219f
equal deleted inserted replaced
5353:ac9710126e1a 5354:f8ec43db580b
       
     1 -- Provide OpenID UserInfo data to mod_http_oauth2
       
     2 -- Alternatively, separate module for the whole HTTP endpoint?
       
     3 --
       
     4 local nodeprep = require "util.encodings".stringprep.nodeprep;
       
     5 
       
     6 local mod_pep = module:depends "pep";
       
     7 
       
     8 local gender_map = { M = "male"; F = "female"; O = "other"; N = "nnot applicable"; U = "unknown" }
       
     9 
       
    10 module:hook("token/userinfo", function(event)
       
    11 	local pep_service = mod_pep.get_pep_service(event.username);
       
    12 
       
    13 	local vcard4 = select(3, pep_service:get_last_item("urn:xmpp:vcard4", true));
       
    14 
       
    15 	local userinfo = event.userinfo;
       
    16 	if vcard4 and event.claims:contains("profile") then
       
    17 		userinfo.name = vcard4:find("fn/text#");
       
    18 		userinfo.family_name = vcard4:find("n/surname#");
       
    19 		userinfo.given_name = vcard4:find("n/given#");
       
    20 		userinfo.middle_name = vcard4:find("n/additional#");
       
    21 
       
    22 		userinfo.nickname = vcard4:find("nickname/text#");
       
    23 		if not userinfo.nickname then
       
    24 			local ok, _, nick_item = pep_service:get_last_item("http://jabber.org/protocol/nick", true);
       
    25 			if ok and nick_item then
       
    26 				userinfo.nickname = nick_item:get_child_text("nick", "http://jabber.org/protocol/nick");
       
    27 			end
       
    28 		end
       
    29 
       
    30 		userinfo.preferred_username = event.username;
       
    31 
       
    32 		-- profile -- page? not their website
       
    33 		-- picture -- mod_http_pep_avatar?
       
    34 		userinfo.website = vcard4:find("url/uri#");
       
    35 		userinfo.birthdate = vcard4:find("bday/date#");
       
    36 		userinfo.zoneinfo = vcard4:find("tz/text#");
       
    37 		userinfo.locale = vcard4:find("lang/language-tag#");
       
    38 
       
    39 		userinfo.gender = gender_map[vcard4:find("gender/sex#")] or vcard4:find("gender/text#");
       
    40 
       
    41 		-- updated_at -- we don't keep a vcard change timestamp?
       
    42 	end
       
    43 
       
    44 	if not userinfo.nickname and event.claims:contains("profile") then
       
    45 		local ok, _, nick_item = pep_service:get_last_item("http://jabber.org/protocol/nick", true);
       
    46 		if ok and nick_item then
       
    47 			userinfo.nickname = nick_item:get_child_text("nick", "http://jabber.org/protocol/nick");
       
    48 		end
       
    49 	end
       
    50 
       
    51 	if vcard4 and event.claims:contains("email") then
       
    52 		userinfo.email = vcard4:find("email/text#")
       
    53 		if userinfo.email then
       
    54 			userinfo.email_verified = false;
       
    55 		end
       
    56 	end
       
    57 
       
    58 	if vcard4 and event.claims:contains("address") then
       
    59 		local adr = vcard4:get_child("adr");
       
    60 		if adr then
       
    61 			userinfo.address = {
       
    62 				formatted = nil;
       
    63 				street_address = adr:get_child_text("street");
       
    64 				locality = adr:get_child_text("locality");
       
    65 				region = adr:get_child_text("region");
       
    66 				postal_code = adr:get_child_text("code");
       
    67 				country = adr:get_child_text("country");
       
    68 			}
       
    69 		end
       
    70 	end
       
    71 
       
    72 	if vcard4 and event.claims:contains("phone") then
       
    73 		userinfo.phone = vcard4:find("email/text#")
       
    74 		if userinfo.phone then
       
    75 			userinfo.phone_number_verified = false;
       
    76 		end
       
    77 	end
       
    78 
       
    79 
       
    80 end, 10);