1111use SimpleSAML \Module \perun \Adapter ;
1212use SimpleSAML \Module \perun \ChallengeManager ;
1313
14- const CLASS_PREFIX = 'perun/www/updateUes.php: ' ;
14+ const DEBUG_PREFIX = 'perun/www/updateUes.php: ' ;
1515const CONFIG_FILE_NAME = 'module_perun.php ' ;
1616const CONFIG_SECTION = 'updateUes ' ;
1717const SOURCE_IDP_ATTRIBUTE_KEY = 'sourceIdPAttributeKey ' ;
4545const NAMEID = 'nameid ' ;
4646const UID = 'uid ' ;
4747
48+ function getDefaultConfig (): array
49+ {
50+ return [
51+ SOURCE_IDP_ATTRIBUTE_KEY => SOURCE_IDP_ENTITY_ID ,
52+ USER_IDENTIFIERS => [EDU_PERSON_UNIQUE_ID , EDU_PERSON_PRINCIPAL_NAME , EDU_PERSON_TARGETED_ID , NAMEID , UID ],
53+ ];
54+ }
55+
56+ function getConfiguration ()
57+ {
58+ $ config = getDefaultConfig ();
59+ try {
60+ $ configuration = Configuration::getConfig (CONFIG_FILE_NAME );
61+ $ localConfig = $ configuration ->getArray (CONFIG_SECTION , null );
62+ if (!empty ($ localConfig )) {
63+ $ config = $ localConfig ;
64+ } else {
65+ Logger::warning (DEBUG_PREFIX . 'Configuration is missing. Using default values ' );
66+ }
67+ } catch (Exception $ e ) {
68+ Logger::warning (DEBUG_PREFIX . 'Configuration is invalid. Using default values ' );
69+ //OK, we will use the default config
70+ }
71+
72+ return $ config ;
73+ }
74+
4875$ adapter = Adapter::getInstance (Adapter::RPC );
4976$ token = file_get_contents ('php://input ' );
5077
5582
5683$ attributesFromIdP = null ;
5784$ attrMap = null ;
58- $ attrsToConversion = null ;
85+ $ serializedAttributes = null ;
5986$ perunUserId = null ;
6087$ id = null ;
61- $ sourceIdpAttributeKey = null ;
88+ $ sourceIdpAttribute = null ;
6289
6390try {
6491 $ challengeManager = new ChallengeManager ();
6592 $ claims = $ challengeManager ->decodeToken ($ token );
6693
6794 $ attributesFromIdP = $ claims [DATA ][ATTRIBUTES ];
6895 $ attrMap = $ claims [DATA ][ATTR_MAP ];
69- $ attrsToConversion = $ claims [DATA ][ATTR_TO_CONVERSION ];
96+ $ serializedAttributes = $ claims [DATA ][ATTR_TO_CONVERSION ];
7097 $ perunUserId = $ claims [DATA ][PERUN_USER_ID ];
7198 $ id = $ claims [ID ];
7299} catch (Exception $ ex ) {
73- Logger::error (CLASS_PREFIX . 'The token verification ended with an error. ' );
100+ Logger::error (DEBUG_PREFIX . 'The token verification ended with an error. ' );
74101 http_response_code (400 );
75102 exit ;
76103}
77104
78- try {
79- $ config = Configuration::getConfig (CONFIG_FILE_NAME );
80- $ config = $ config ->getArray (CONFIG_SECTION , null );
81- } catch (Exception $ e ) {
82- $ config = null ;
83- }
105+ $ config = getConfiguration ();
84106
85- if (null === $ config ) {
86- Logger::warning (CLASS_PREFIX . 'Configuration is missing. Using default values ' );
87- }
88-
89- $ sourceIdpAttributeKey = empty ($ config [SOURCE_IDP_ATTRIBUTE_KEY ]) ? SOURCE_IDP_ENTITY_ID : $ config [SOURCE_IDP_ATTRIBUTE_KEY ];
90-
91- if (null !== $ config && !empty ($ config [USER_IDENTIFIERS ] && is_array ($ config [USER_IDENTIFIERS ]))) {
92- $ userIdentifiers = $ config [USER_IDENTIFIERS ];
93- } else {
94- $ userIdentifiers = [EDU_PERSON_UNIQUE_ID , EDU_PERSON_PRINCIPAL_NAME , EDU_PERSON_TARGETED_ID , NAMEID , UID ];
95- }
107+ $ sourceIdpAttribute = $ config [SOURCE_IDP_ATTRIBUTE_KEY ];
108+ $ identifierAttributes = $ config [USER_IDENTIFIERS ];
96109
97110try {
98- if (empty ($ attributesFromIdP [$ sourceIdpAttributeKey ][0 ])) {
99- throw new Exception (CLASS_PREFIX . 'Invalid attributes from Idp - \'' . $ sourceIdpAttributeKey . '\' is empty ' );
111+ if (empty ($ attributesFromIdP [$ sourceIdpAttribute ][0 ])) {
112+ throw new Exception (
113+ DEBUG_PREFIX . 'Invalid attributes from IdP - Attribute \'' . $ sourceIdpAttribute . '\' is empty '
114+ );
100115 }
101116
102- $ extSourceName = $ attributesFromIdP [$ sourceIdpAttributeKey ][0 ];
103- Logger::debug (CLASS_PREFIX . 'Extracted extSourceName: \'' . $ extSourceName . '\'' );
117+ $ extSourceName = $ attributesFromIdP [$ sourceIdpAttribute ][0 ];
118+ Logger::debug (DEBUG_PREFIX . 'Extracted extSourceName: \'' . $ extSourceName . '\'' );
104119
105- $ userExtSource = findUserExtSource ($ adapter , $ extSourceName , $ attributesFromIdP , $ userIdentifiers );
120+ $ userExtSource = findUserExtSource ($ adapter , $ extSourceName , $ attributesFromIdP , $ identifierAttributes );
106121 if (null === $ userExtSource ) {
107122 throw new Exception (
108- CLASS_PREFIX . 'There is no UserExtSource that could be used for user ' . $ perunUserId . ' and ExtSource ' . $ attributesFromIdP [ $ sourceIdpAttributeKey ][ 0 ]
123+ DEBUG_PREFIX . 'There is no UserExtSource that could be used for user ' . $ perunUserId . ' and IdP ' . $ extSourceName
109124 );
110125 }
111126
112127 $ attributesFromPerun = getAttributesFromPerun ($ adapter , $ attrMap , $ userExtSource );
113- $ attributesToUpdate = getAttributesToUpdate ($ attributesFromPerun , $ attrMap , $ attrsToConversion , $ attributesFromIdP );
128+ $ attributesToUpdate = getAttributesToUpdate (
129+ $ attributesFromPerun ,
130+ $ attrMap ,
131+ $ serializedAttributes ,
132+ $ attributesFromIdP
133+ );
114134
115135 if (updateUserExtSource ($ adapter , $ userExtSource , $ attributesToUpdate )) {
116- Logger::debug (CLASS_PREFIX . 'Updating UES for user with userId: ' . $ perunUserId . ' was successful. ' );
136+ Logger::debug (DEBUG_PREFIX . 'Updating UES for user with userId: ' . $ perunUserId . ' was successful. ' );
117137 }
118138} catch (\Exception $ ex ) {
119139 Logger::warning (
120- CLASS_PREFIX . 'Updating UES for user with userId: ' . $ perunUserId . ' was not successful: ' .
140+ DEBUG_PREFIX . 'Updating UES for user with userId: ' . $ perunUserId . ' was not successful: ' .
121141 $ ex ->getMessage ()
122142 );
123143}
124144
125- function findUserExtSource ($ adapter , $ extSourceName , $ attributes , $ userIdentifiers )
145+ function findUserExtSource ($ adapter , $ extSourceName , $ attributesFromIdp , $ identifierAttributes )
126146{
127- foreach ($ attributes as $ attrName => $ attrValue ) {
128- if (!in_array ($ attrName , $ userIdentifiers , true )) {
129- Logger::debug (CLASS_PREFIX . 'Identifier \'' . $ attrName . '\' not listed in userIdentifiers. Skipping ' );
147+ foreach ($ attributesFromIdp as $ attrName => $ attrValue ) {
148+ if (!in_array ($ attrName , $ identifierAttributes , true )) {
149+ Logger::debug (DEBUG_PREFIX . 'Identifier \'' . $ attrName . '\' not listed in userIdentifiers. Skipping ' );
130150 continue ;
131151 }
132152
133- if (is_array ($ attrValue )) {
134- foreach ( $ attrValue as $ extLogin ) {
135- $ userExtSource = getUserExtSource ( $ adapter , $ extSourceName , $ extLogin );
153+ if (! is_array ($ attrValue )) {
154+ $ attrValue = [ $ attrValue ];
155+ }
136156
137- if (null !== $ userExtSource ) {
138- return $ userExtSource ;
139- }
140- }
141- } elseif (is_string ($ attrValue )) {
142- $ userExtSource = getUserExtSource ($ adapter , $ attrValue , $ extLogin );
157+ foreach ($ attrValue as $ extLogin ) {
158+ $ userExtSource = getUserExtSource ($ adapter , $ extSourceName , $ extLogin );
143159
144160 if (null !== $ userExtSource ) {
161+ Logger::debug (
162+ DEBUG_PREFIX . 'Found user ext source for combination extSourceName \''
163+ . $ extSourceName . '\' and extLogin \'' . $ extLogin . '\''
164+ );
165+
145166 return $ userExtSource ;
146167 }
147168 }
@@ -155,32 +176,35 @@ function getUserExtSource($adapter, $extSourceName, $extLogin)
155176 try {
156177 return $ adapter ->getUserExtSource ($ extSourceName , $ extLogin );
157178 } catch (SimpleSAML \Module \perun \Exception $ ex ) {
158- Logger::debug (CLASS_PREFIX . 'Caught exception when fetching user ext source, probably does not exist. ' );
159- Logger::debug (CLASS_PREFIX . $ ex ->getMessage ());
179+ Logger::debug (DEBUG_PREFIX . 'Caught exception when fetching user ext source, probably does not exist. ' );
180+ Logger::debug (DEBUG_PREFIX . $ ex ->getMessage ());
160181
161182 return null ;
162183 }
163184}
164185
165186function getAttributesFromPerun ($ adapter , $ attrMap , $ userExtSource ): array
166187{
167- $ attributesFromPerunRaw = $ adapter ->getUserExtSourceAttributes ($ userExtSource [ID ], array_keys ($ attrMap ));
168188 $ attributesFromPerun = [];
189+ $ attributesFromPerunRaw = $ adapter ->getUserExtSourceAttributes ($ userExtSource [ID ], array_keys ($ attrMap ));
190+ if (empty ($ attributesFromPerunRaw )) {
191+ throw new Exception (DEBUG_PREFIX . 'Getting attributes for UES was not successful. ' );
192+ }
169193
170194 foreach ($ attributesFromPerunRaw as $ rawAttribute ) {
171195 if (!empty ($ rawAttribute [NAME ])) {
172196 $ attributesFromPerun [$ rawAttribute [NAME ]] = $ rawAttribute ;
173197 }
174198 }
175199
176- if (null === $ attributesFromPerun ) {
177- throw new Exception (CLASS_PREFIX . 'Getting attributes was not successful. ' );
200+ if (empty ( $ attributesFromPerun) ) {
201+ throw new Exception (DEBUG_PREFIX . 'Getting attributes for UES was not successful. ' );
178202 }
179203
180204 return $ attributesFromPerun ;
181205}
182206
183- function getAttributesToUpdate ($ attributesFromPerun , $ attrMap , $ attrsToConversion , $ attributesFromIdP ): array
207+ function getAttributesToUpdate ($ attributesFromPerun , $ attrMap , $ serializedAttributes , $ attributesFromIdP ): array
184208{
185209 $ attributesToUpdate = [];
186210
@@ -192,7 +216,7 @@ function getAttributesToUpdate($attributesFromPerun, $attrMap, $attrsToConversio
192216 $ attributesFromIdP [$ attrMap [$ attrName ]] : null ;
193217
194218 if (null !== $ mappedAttributeName && null !== $ idpAttribute ) {
195- if (in_array ($ attrName , $ attrsToConversion , true )) {
219+ if (in_array ($ attrName , $ serializedAttributes , true )) {
196220 $ idpAttribute = serializeAsString ($ idpAttribute );
197221 }
198222
@@ -201,7 +225,8 @@ function getAttributesToUpdate($attributesFromPerun, $attrMap, $attrsToConversio
201225 } elseif (isComplexType ($ attribute [TYPE ])) {
202226 $ valueFromIdP = $ idpAttribute ;
203227 } else {
204- throw new Exception (CLASS_PREFIX . 'Unsupported type of attribute. ' );
228+ Logger::debug (DEBUG_PREFIX . 'Unsupported type of attribute. ' );
229+ continue ;
205230 }
206231
207232 if ($ valueFromIdP !== $ attribute [VALUE ]) {
@@ -246,7 +271,7 @@ function isComplexType($attributeType): bool
246271 strpos ($ attributeType , MAP_TYPE );
247272}
248273
249- function serializeAsString ($ idpAttribute )
274+ function serializeAsString ($ idpAttribute ): array
250275{
251276 $ arrayAsString = ['' ];
252277
0 commit comments