@@ -45,172 +45,132 @@ public function __construct(
4545 */
4646 public function verifiableCredentialIssuance (Request $ request ): Response
4747 {
48- $ setupErrors = [];
49-
5048 if (!$ this ->moduleConfig ->getVciEnabled ()) {
51- $ setupErrors [] = 'Verifiable Credential functionalities are not enabled. ' ;
52- }
53-
54- $ selectedAuthSourceId = $ this ->sessionService ->getCurrentSession ()->getData ('vci ' , 'auth_source_id ' );
55-
56- $ authSource = null ;
57- if (is_string ($ selectedAuthSourceId )) {
58- $ authSource = $ this ->authSimpleFactory ->forAuthSourceId ($ selectedAuthSourceId );
49+ return $ this ->templateFactory ->build (
50+ 'oidc:tests/verifiable-credential-issuance.twig ' ,
51+ ['setupErrors ' => ['Verifiable Credential functionalities are not enabled. ' ]],
52+ RoutesEnum::AdminTestVerifiableCredentialIssuance->value ,
53+ );
5954 }
6055
61- $ allowedMethods = [
62- HttpMethodsEnum::GET ,
63- HttpMethodsEnum::POST ,
64- ];
65-
66- // Check if the logout was called.
67- if (
68- $ request ->request ->has ('logout ' ) &&
69- $ authSource instanceof Simple &&
70- $ authSource ->isAuthenticated ()
71- ) {
72- $ this ->sessionService ->getCurrentSession ()->deleteData ('vci ' , 'auth_source_id ' );
73- $ selectedAuthSourceId = null ;
74- $ authSource ->logout ();
75- } elseif (
76- is_string ($ newAuthSourceId = $ this ->requestParamsResolver ->getFromRequestBasedOnAllowedMethods (
77- 'authSourceId ' ,
78- $ request ,
79- $ allowedMethods ,
80- ))
81- ) {
82- $ authSource = $ this ->authSimpleFactory ->forAuthSourceId ($ newAuthSourceId );
83- $ this ->sessionService ->getCurrentSession ()->setData ('vci ' , 'auth_source_id ' , $ newAuthSourceId );
84- $ selectedAuthSourceId = $ newAuthSourceId ;
56+ $ credentialConfigurationIdsSupported = $ this ->moduleConfig ->getVciCredentialConfigurationIdsSupported ();
57+ if (empty ($ credentialConfigurationIdsSupported )) {
58+ return $ this ->templateFactory ->build (
59+ 'oidc:tests/verifiable-credential-issuance.twig ' ,
60+ ['setupErrors ' => ['No credential configuration IDs configured. ' ]],
61+ RoutesEnum::AdminTestVerifiableCredentialIssuance->value ,
62+ );
8563 }
8664
87- $ authSourceIds = array_filter (
88- $ this ->sspBridge ->auth ()->source ()->getSources (),
89- fn (string $ authSourceId ): bool => $ authSourceId !== 'admin ' ,
90- );
91-
92- if (
93- $ authSource instanceof Simple &&
94- ($ authSource ->isAuthenticated () === false ) &&
95- is_string ($ selectedAuthSourceId ) &&
96- in_array ($ selectedAuthSourceId , $ authSourceIds , true )
97- ) {
98- $ authSource ->login (['ReturnTo ' => $ this ->routes ->urlAdminTestVerifiableCredentialIssuance ()]);
99- }
65+ $ session = $ this ->sessionService ->getCurrentSession ();
66+ $ allowedMethods = [HttpMethodsEnum::GET , HttpMethodsEnum::POST ];
10067
101- /** @psalm-suppress MixedAssignment */
102- $ selectedCredentialConfigurationId = $ this ->sessionService ->getCurrentSession ()->getData (
103- 'vci ' ,
104- 'credential_configuration_id ' ,
105- );
68+ if ($ request ->request ->has ('clear ' )) {
69+ $ selectedAuthSourceId = $ session ->getData ('vci ' , 'auth_source_id ' );
70+ if (is_string ($ selectedAuthSourceId )) {
71+ $ authSource = $ this ->authSimpleFactory ->forAuthSourceId ($ selectedAuthSourceId );
72+ if ($ authSource ->isAuthenticated ()) {
73+ $ authSource ->logout ();
74+ }
75+ }
76+ $ session ->deleteData ('vci ' , 'auth_source_id ' );
77+ $ session ->deleteData ('vci ' , 'credential_configuration_id ' );
10678
107- /** @psalm-suppress MixedAssignment, InternalMethod */
108- if (
109- is_string ($ newCredentialConfigurationId = $ this ->requestParamsResolver ->getFromRequestBasedOnAllowedMethods (
110- 'credentialConfigurationId ' ,
111- $ request ,
112- $ allowedMethods ,
113- ))
114- ) {
115- $ this ->sessionService ->getCurrentSession ()->setData (
116- 'vci ' ,
117- 'credential_configuration_id ' ,
118- $ newCredentialConfigurationId ,
79+ return $ this ->routes ->newRedirectResponseToModuleUrl (
80+ RoutesEnum::AdminTestVerifiableCredentialIssuance->value ,
11981 );
120- $ selectedCredentialConfigurationId = $ newCredentialConfigurationId ;
12182 }
12283
123- $ credentialConfigurationIdsSupported = $ this ->moduleConfig -> getVciCredentialConfigurationIdsSupported ();
124-
125- if ( empty ( $ credentialConfigurationIdsSupported )) {
126- $ setupErrors [] = ' No credential configuration IDs configured. ' ;
127- }
84+ $ authSourceId = $ this ->requestParamsResolver -> getFromRequestBasedOnAllowedMethods (
85+ ' authSourceId ' ,
86+ $ request ,
87+ $ allowedMethods ,
88+ ) ?? $ session -> getData ( ' vci ' , ' auth_source_id ' );
12889
129- if (
130- is_null ($ selectedCredentialConfigurationId ) ||
131- !in_array ($ selectedCredentialConfigurationId , $ credentialConfigurationIdsSupported , true )
132- ) {
133- $ selectedCredentialConfigurationId = current ($ credentialConfigurationIdsSupported );
134- }
90+ $ credentialConfigurationId = $ this ->requestParamsResolver ->getFromRequestBasedOnAllowedMethods (
91+ 'credentialConfigurationId ' ,
92+ $ request ,
93+ $ allowedMethods ,
94+ ) ?? $ session ->getData ('vci ' , 'credential_configuration_id ' );
13595
136- $ credentialOfferQrUri = null ;
137- $ credentialOfferUri = null ;
13896 $ grantType = $ this ->requestParamsResolver ->getFromRequestBasedOnAllowedMethods (
13997 'grantType ' ,
14098 $ request ,
14199 $ allowedMethods ,
142100 );
101+
143102 $ useTxCode = (bool ) $ this ->requestParamsResolver ->getFromRequestBasedOnAllowedMethods (
144103 'useTxCode ' ,
145104 $ request ,
146105 $ allowedMethods ,
147106 );
107+
148108 $ usersEmailAttributeName = $ this ->requestParamsResolver ->getFromRequestBasedOnAllowedMethods (
149109 'usersEmailAttributeName ' ,
150110 $ request ,
151111 $ allowedMethods ,
152112 );
153- $ usersEmailAttributeName = is_string ($ usersEmailAttributeName ) && (trim ($ usersEmailAttributeName ) !== '' ) ?
154- trim ($ usersEmailAttributeName ) :
155- null ;
156-
157- if (
158- $ authSource instanceof Simple &&
159- $ authSource ->isAuthenticated ()
160- ) {
161- $ userAttributes = $ authSource ->getAttributes ();
162- $ usersEmailAttributeName ??= $ this ->moduleConfig ->getUsersEmailAttributeNameForAuthSourceId (
163- $ authSource ->getAuthSource ()->getAuthId (),
164- );
165113
166- if (
167- $ grantType === GrantTypesEnum::PreAuthorizedCode->value &&
168- is_string ($ selectedCredentialConfigurationId )
169- ) {
114+ $ authSourceIds = array_filter (
115+ $ this ->sspBridge ->auth ()->source ()->getSources (),
116+ fn (string $ id ): bool => $ id !== 'admin ' ,
117+ );
118+
119+ $ authSource = is_string ($ authSourceId ) ? $ this ->authSimpleFactory ->forAuthSourceId ($ authSourceId ) : null ;
120+
121+ if ($ authSource instanceof Simple && $ grantType === GrantTypesEnum::PreAuthorizedCode->value ) {
122+ if (!$ authSource ->isAuthenticated ()) {
123+ $ session ->setData ('vci ' , 'auth_source_id ' , $ authSourceId );
124+ $ session ->setData ('vci ' , 'credential_configuration_id ' , $ credentialConfigurationId );
125+ $ authSource ->login (['ReturnTo ' => $ this ->routes ->urlAdminTestVerifiableCredentialIssuance ()]);
126+ }
127+ }
128+
129+ $ credentialOfferUri = null ;
130+ if (is_string ($ credentialConfigurationId )) {
131+ if ($ grantType === GrantTypesEnum::PreAuthorizedCode->value && $ authSource ?->isAuthenticated()) {
132+ $ usersEmailAttributeName = is_string ($ usersEmailAttributeName ) &&
133+ trim ($ usersEmailAttributeName ) !== '' ?
134+ trim ($ usersEmailAttributeName ) :
135+ $ this ->moduleConfig ->getUsersEmailAttributeNameForAuthSourceId (
136+ is_string ($ authSourceId ) ? $ authSourceId : '' ,
137+ );
138+
170139 $ credentialOfferUri = $ this ->credentialOfferUriFactory ->buildPreAuthorized (
171- [$ selectedCredentialConfigurationId ],
172- $ userAttributes ,
140+ [$ credentialConfigurationId ],
141+ $ authSource -> getAttributes () ,
173142 $ useTxCode ,
174143 $ usersEmailAttributeName ,
175144 );
176- } elseif (is_string ( $ selectedCredentialConfigurationId ) ) {
145+ } elseif ($ grantType === GrantTypesEnum::AuthorizationCode-> value ) {
177146 $ credentialOfferUri = $ this ->credentialOfferUriFactory ->buildForAuthorization (
178- [$ selectedCredentialConfigurationId ],
147+ [$ credentialConfigurationId ],
179148 );
180149 }
181-
182- // TODO mivanci Local QR code generator
183- // https://quickchart.io/documentation/qr-codes/
184- if (is_string ($ credentialOfferUri )) {
185- $ credentialOfferQrUri = 'https://quickchart.io/qr?size=200&margin=1&text= ' .
186- urlencode ($ credentialOfferUri );
187- }
188150 }
189151
190- $ authSourceActionRoute = $ this ->routes ->urlAdminTestVerifiableCredentialIssuance ();
191-
192- $ defaultUsersEmailAttributeName = $ this ->moduleConfig ->getDefaultUsersEmailAttributeName ();
193-
194- $ grantTypesSupported = [
195- GrantTypesEnum::PreAuthorizedCode->value => Translate::noop ('Pre-authorized Code ' ),
196- GrantTypesEnum::AuthorizationCode->value => Translate::noop ('Authorization Code ' ),
197- ];
152+ $ credentialOfferQrUri = is_string ($ credentialOfferUri )
153+ ? 'https://quickchart.io/qr?size=200&margin=1&text= ' . urlencode ($ credentialOfferUri )
154+ : null ;
198155
199156 return $ this ->templateFactory ->build (
200157 'oidc:tests/verifiable-credential-issuance.twig ' ,
201- compact (
202- 'setupErrors ' ,
203- 'credentialOfferQrUri ' ,
204- 'credentialOfferUri ' ,
205- 'authSourceIds ' ,
206- 'authSourceActionRoute ' ,
207- 'authSource ' ,
208- 'credentialConfigurationIdsSupported ' ,
209- 'selectedCredentialConfigurationId ' ,
210- 'defaultUsersEmailAttributeName ' ,
211- 'usersEmailAttributeName ' ,
212- 'grantTypesSupported ' ,
213- ),
158+ [
159+ 'setupErrors ' => [],
160+ 'credentialOfferQrUri ' => $ credentialOfferQrUri ,
161+ 'credentialOfferUri ' => $ credentialOfferUri ,
162+ 'authSourceIds ' => $ authSourceIds ,
163+ 'authSourceActionRoute ' => $ this ->routes ->urlAdminTestVerifiableCredentialIssuance (),
164+ 'authSource ' => $ authSource ,
165+ 'credentialConfigurationIdsSupported ' => $ credentialConfigurationIdsSupported ,
166+ 'selectedCredentialConfigurationId ' => $ credentialConfigurationId ,
167+ 'defaultUsersEmailAttributeName ' => $ this ->moduleConfig ->getDefaultUsersEmailAttributeName (),
168+ 'usersEmailAttributeName ' => $ usersEmailAttributeName ,
169+ 'grantTypesSupported ' => [
170+ GrantTypesEnum::PreAuthorizedCode->value => Translate::noop ('Pre-authorized Code ' ),
171+ GrantTypesEnum::AuthorizationCode->value => Translate::noop ('Authorization Code ' ),
172+ ],
173+ ],
214174 RoutesEnum::AdminTestVerifiableCredentialIssuance->value ,
215175 );
216176 }
0 commit comments