@@ -15,89 +15,165 @@ limitations under the License.
1515*/
1616
1717import { M_AUTHENTICATION } from "../../../src" ;
18- import { OidcDiscoveryError , validateWellKnownAuthentication } from "../../../src/oidc/validate" ;
18+ import { logger } from "../../../src/logger" ;
19+ import {
20+ OidcDiscoveryError ,
21+ validateOIDCIssuerWellKnown ,
22+ validateWellKnownAuthentication ,
23+ } from "../../../src/oidc/validate" ;
1924
20- describe ( ' validateWellKnownAuthentication()' , ( ) => {
25+ describe ( " validateWellKnownAuthentication()" , ( ) => {
2126 const baseWk = {
22- "m.homeserver" : {
23- base_url : "https://hs.org"
24- }
25- }
26- it ( ' should throw not supported error when wellKnown has no m.authentication section' , ( ) => {
27+ "m.homeserver" : {
28+ base_url : "https://hs.org" ,
29+ } ,
30+ } ;
31+ it ( " should throw not supported error when wellKnown has no m.authentication section" , ( ) => {
2732 expect ( ( ) => validateWellKnownAuthentication ( baseWk ) ) . toThrow ( OidcDiscoveryError . NotSupported ) ;
2833 } ) ;
2934
30- it ( ' should throw misconfigured error when authentication issuer is not a string' , ( ) => {
35+ it ( " should throw misconfigured error when authentication issuer is not a string" , ( ) => {
3136 const wk = {
3237 ...baseWk ,
3338 [ M_AUTHENTICATION . stable ! ] : {
34- issuer : { url : ' test.com' }
35- }
36- }
39+ issuer : { url : " test.com" } ,
40+ } ,
41+ } ;
3742 expect ( ( ) => validateWellKnownAuthentication ( wk ) ) . toThrow ( OidcDiscoveryError . Misconfigured ) ;
3843 } ) ;
3944
40- it ( ' should throw misconfigured error when authentication account is not a string' , ( ) => {
45+ it ( " should throw misconfigured error when authentication account is not a string" , ( ) => {
4146 const wk = {
4247 ...baseWk ,
4348 [ M_AUTHENTICATION . stable ! ] : {
4449 issuer : "test.com" ,
45- account : { url : "test" }
46- }
47- }
50+ account : { url : "test" } ,
51+ } ,
52+ } ;
4853 expect ( ( ) => validateWellKnownAuthentication ( wk ) ) . toThrow ( OidcDiscoveryError . Misconfigured ) ;
4954 } ) ;
5055
51- it ( ' should return valid config when wk uses stable m.authentication' , ( ) => {
56+ it ( " should return valid config when wk uses stable m.authentication" , ( ) => {
5257 const wk = {
5358 ...baseWk ,
5459 [ M_AUTHENTICATION . stable ! ] : {
5560 issuer : "test.com" ,
5661 account : "account.com" ,
57- }
58- }
62+ } ,
63+ } ;
5964 expect ( validateWellKnownAuthentication ( wk ) ) . toEqual ( {
6065 issuer : "test.com" ,
61- account : "account.com"
66+ account : "account.com" ,
6267 } ) ;
6368 } ) ;
6469
65- it ( ' should return valid config when m.authentication account is falsy' , ( ) => {
70+ it ( " should return valid config when m.authentication account is falsy" , ( ) => {
6671 const wk = {
6772 ...baseWk ,
6873 [ M_AUTHENTICATION . stable ! ] : {
6974 issuer : "test.com" ,
70- }
71- }
75+ } ,
76+ } ;
7277 expect ( validateWellKnownAuthentication ( wk ) ) . toEqual ( {
7378 issuer : "test.com" ,
7479 } ) ;
7580 } ) ;
7681
77- it ( ' should remove unexpected properties' , ( ) => {
82+ it ( " should remove unexpected properties" , ( ) => {
7883 const wk = {
7984 ...baseWk ,
8085 [ M_AUTHENTICATION . stable ! ] : {
8186 issuer : "test.com" ,
82- somethingElse : "test"
83- }
84- }
87+ somethingElse : "test" ,
88+ } ,
89+ } ;
8590 expect ( validateWellKnownAuthentication ( wk ) ) . toEqual ( {
8691 issuer : "test.com" ,
8792 } ) ;
8893 } ) ;
8994
90- it ( ' should return valid config when wk uses unstable prefix for m.authentication' , ( ) => {
95+ it ( " should return valid config when wk uses unstable prefix for m.authentication" , ( ) => {
9196 const wk = {
9297 ...baseWk ,
9398 [ M_AUTHENTICATION . unstable ! ] : {
9499 issuer : "test.com" ,
95100 account : "account.com" ,
96- }
97- }
101+ } ,
102+ } ;
98103 expect ( validateWellKnownAuthentication ( wk ) ) . toEqual ( {
99104 issuer : "test.com" ,
100- account : "account.com"
105+ account : "account.com" ,
101106 } ) ;
102107 } ) ;
103- } ) ;
108+ } ) ;
109+
110+ describe ( "validateOIDCIssuerWellKnown" , ( ) => {
111+ const validWk = {
112+ authorization_endpoint : "https://test.org/authorize" ,
113+ token_endpoint : "https://authorize.org/token" ,
114+ registration_endpoint : "https://authorize.org/regsiter" ,
115+ response_types_supported : [ "code" ] ,
116+ grant_types_supported : [ "authorization_code" ] ,
117+ code_challenge_methods_supported : [ "S256" ] ,
118+ } ;
119+ beforeEach ( ( ) => {
120+ // stub to avoid console litter
121+ jest . spyOn ( logger , "error" )
122+ . mockClear ( )
123+ . mockImplementation ( ( ) => { } ) ;
124+ } ) ;
125+
126+ it ( "should throw OP support error when wellKnown is not an object" , ( ) => {
127+ expect ( ( ) => {
128+ validateOIDCIssuerWellKnown ( [ ] ) ;
129+ } ) . toThrow ( OidcDiscoveryError . OpSupport ) ;
130+ expect ( logger . error ) . toHaveBeenCalledWith ( "Issuer configuration not found or malformed" ) ;
131+ } ) ;
132+
133+ it ( "should log all errors before throwing" , ( ) => {
134+ expect ( ( ) => {
135+ validateOIDCIssuerWellKnown ( {
136+ ...validWk ,
137+ authorization_endpoint : undefined ,
138+ response_types_supported : [ ] ,
139+ } ) ;
140+ } ) . toThrow ( OidcDiscoveryError . OpSupport ) ;
141+ expect ( logger . error ) . toHaveBeenCalledWith ( "OIDC issuer configuration: authorization_endpoint is invalid" ) ;
142+ expect ( logger . error ) . toHaveBeenCalledWith (
143+ "OIDC issuer configuration: response_types_supported is invalid. code is required." ,
144+ ) ;
145+ } ) ;
146+
147+ it ( "should return validated issuer config" , ( ) => {
148+ expect ( validateOIDCIssuerWellKnown ( validWk ) ) . toEqual ( {
149+ authorizationEndpoint : validWk . authorization_endpoint ,
150+ tokenEndpoint : validWk . token_endpoint ,
151+ registrationEndpoint : validWk . registration_endpoint ,
152+ } ) ;
153+ } ) ;
154+
155+ type TestCase = [ string , any ] ;
156+ it . each < TestCase > ( [
157+ [ "authorization_endpoint" , undefined ] ,
158+ [ "authorization_endpoint" , { not : "a string" } ] ,
159+ [ "token_endpoint" , undefined ] ,
160+ [ "token_endpoint" , { not : "a string" } ] ,
161+ [ "registration_endpoint" , undefined ] ,
162+ [ "registration_endpoint" , { not : "a string" } ] ,
163+ [ "response_types_supported" , undefined ] ,
164+ [ "response_types_supported" , "not an array" ] ,
165+ [ "response_types_supported" , [ "doesnt include code" ] ] ,
166+ [ "grant_types_supported" , undefined ] ,
167+ [ "grant_types_supported" , "not an array" ] ,
168+ [ "grant_types_supported" , [ "doesnt include authorization_code" ] ] ,
169+ [ "code_challenge_methods_supported" , undefined ] ,
170+ [ "code_challenge_methods_supported" , "not an array" ] ,
171+ [ "code_challenge_methods_supported" , [ "doesnt include S256" ] ] ,
172+ ] ) ( "should throw OP support error when %s is %s" , ( key , value ) => {
173+ const wk = {
174+ ...validWk ,
175+ [ key ] : value ,
176+ } ;
177+ expect ( ( ) => validateOIDCIssuerWellKnown ( wk ) ) . toThrow ( OidcDiscoveryError . OpSupport ) ;
178+ } ) ;
179+ } ) ;
0 commit comments