@@ -41,38 +41,39 @@ var (
41
41
testPlatformInstanceID = uuid .MustParse ("52166f98-f932-4ccf-ae71-e0ae10255e4f" )
42
42
testRegistrationID = uuid .MustParse ("7b556115-9460-4f1e-835e-cb11a7301f7d" )
43
43
testLaunchWithDeploymentID = uuid .MustParse ("65ec0a8c-48e2-423b-b6e0-d1143292d550" )
44
- testStoreSvc * mockStoreSvc
44
+ testJwkKey jwk. Key
45
45
testSrvUrl string
46
46
)
47
47
48
48
func TestMain (m * testing.M ) {
49
49
// Setup a mock JWK keyset and server
50
50
key , _ := jwk .FromRaw ([]byte (testJWTSecret ))
51
- err := key .Set ("kid" , "testkey" )
51
+ err := key .Set (jwk . KeyIDKey , "testkey" )
52
52
if err != nil {
53
53
panic (err )
54
54
}
55
- err = key .Set ("alg" , "HS256" )
55
+ err = key .Set (jwk . AlgorithmKey , "HS256" )
56
56
if err != nil {
57
57
panic (err )
58
58
}
59
+ keys := make ([]jwk.Key , 0 )
60
+
61
+ k , err := key .PublicKey ()
62
+ if err != nil {
63
+ panic (err )
64
+ }
65
+ keys = append (keys , k )
66
+ type jwkResponse struct {
67
+ Keys []jwk.Key `json:"keys"`
68
+ }
59
69
60
70
srv := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
61
- if r .URL .String () != "/canvaslms/api/lti/security/jwks" {
71
+ if r .URL .String () != canvasTestJWKURL {
62
72
w .WriteHeader (http .StatusInternalServerError )
63
73
return
64
74
}
65
- type response struct {
66
- Keys []jwk.Key `json:"keys"`
67
- }
68
- keys := make ([]jwk.Key , 0 )
69
75
70
- k , err := key .PublicKey ()
71
- if err != nil {
72
- panic (err )
73
- }
74
- keys = append (keys , k )
75
- resp := response {
76
+ resp := jwkResponse {
76
77
Keys : keys ,
77
78
}
78
79
ks , _ := json .Marshal (resp )
@@ -82,13 +83,9 @@ func TestMain(m *testing.M) {
82
83
panic (err )
83
84
}
84
85
}))
86
+ defer srv .Close ()
85
87
86
- testStoreSvc = & mockStoreSvc {
87
- idTokenKey : key ,
88
- jwkServerURL : srv .URL ,
89
- server : srv ,
90
- }
91
-
88
+ testJwkKey = key
92
89
testSrvUrl = srv .URL
93
90
happyPathPlatform = peregrine.Platform {
94
91
ID : testPlatformID ,
@@ -107,7 +104,7 @@ func TestHandleOidcLoginHappyPath(t *testing.T) {
107
104
launchSvc := New (Config {
108
105
JWTKeySecret : testJWTSecret ,
109
106
Issuer : testIssuer ,
110
- }, testStoreSvc )
107
+ }, & mockStoreSvc {} )
111
108
112
109
resp , err := launchSvc .HandleOidcLogin (context .Background (), peregrine.OIDCLoginRequestParams {
113
110
Issuer : canvasTestIssuer ,
@@ -157,7 +154,7 @@ func TestHandleOidcLoginHappyPathWithLTIMessageHint(t *testing.T) {
157
154
launchSvc := New (Config {
158
155
JWTKeySecret : testJWTSecret ,
159
156
Issuer : testIssuer ,
160
- }, testStoreSvc )
157
+ }, & mockStoreSvc {} )
161
158
162
159
resp , err := launchSvc .HandleOidcLogin (context .Background (), peregrine.OIDCLoginRequestParams {
163
160
Issuer : canvasTestIssuer ,
@@ -207,7 +204,7 @@ func TestHandleOidcLoginHappyPathWithDeploymentID(t *testing.T) {
207
204
launchSvc := New (Config {
208
205
JWTKeySecret : testJWTSecret ,
209
206
Issuer : testIssuer ,
210
- }, testStoreSvc )
207
+ }, & mockStoreSvc {} )
211
208
212
209
resp , err := launchSvc .HandleOidcLogin (context .Background (), peregrine.OIDCLoginRequestParams {
213
210
Issuer : canvasTestIssuer ,
@@ -257,7 +254,7 @@ func TestHandleOidcLoginInvalidParams(t *testing.T) {
257
254
launchSvc := New (Config {
258
255
JWTKeySecret : testJWTSecret ,
259
256
Issuer : testIssuer ,
260
- }, testStoreSvc )
257
+ }, & mockStoreSvc {} )
261
258
262
259
_ , err := launchSvc .HandleOidcLogin (context .Background (), peregrine.OIDCLoginRequestParams {
263
260
Issuer : canvasTestIssuer ,
@@ -297,7 +294,7 @@ func TestHandleOidcLoginIncorrectIssuer(t *testing.T) {
297
294
launchSvc := New (Config {
298
295
JWTKeySecret : testJWTSecret ,
299
296
Issuer : testIssuer ,
300
- }, testStoreSvc )
297
+ }, & mockStoreSvc {} )
301
298
302
299
_ , err := launchSvc .HandleOidcLogin (context .Background (), peregrine.OIDCLoginRequestParams {
303
300
Issuer : "https://canvas.instructure.com" ,
@@ -357,7 +354,7 @@ func TestHandleOidcCallbackHappyPath(t *testing.T) {
357
354
launchSvc := New (Config {
358
355
JWTKeySecret : testJWTSecret ,
359
356
Issuer : testIssuer ,
360
- }, testStoreSvc )
357
+ }, & mockStoreSvc {} )
361
358
362
359
state , err := createLaunchState (launchSvc .config .Issuer , launchSvc .config .JWTKeySecret , testLaunchID )
363
360
if err != nil {
@@ -380,7 +377,7 @@ func TestHandleOidcCallbackHappyPath(t *testing.T) {
380
377
if err != nil {
381
378
panic (err )
382
379
}
383
- signedIdToken , err := jwt .Sign (tok , jwt .WithKey (jwa .HS256 , testStoreSvc . idTokenKey ))
380
+ signedIdToken , err := jwt .Sign (tok , jwt .WithKey (jwa .HS256 , testJwkKey ))
384
381
if err != nil {
385
382
panic (err )
386
383
}
@@ -403,7 +400,7 @@ func TestHandleOidcCallbackHappyPathWithLaunchDeploymentID(t *testing.T) {
403
400
launchSvc := New (Config {
404
401
JWTKeySecret : testJWTSecret ,
405
402
Issuer : testIssuer ,
406
- }, testStoreSvc )
403
+ }, & mockStoreSvc {} )
407
404
408
405
state , err := createLaunchState (launchSvc .config .Issuer , launchSvc .config .JWTKeySecret , testLaunchWithDeploymentID )
409
406
if err != nil {
@@ -426,7 +423,7 @@ func TestHandleOidcCallbackHappyPathWithLaunchDeploymentID(t *testing.T) {
426
423
if err != nil {
427
424
panic (err )
428
425
}
429
- signedIdToken , err := jwt .Sign (tok , jwt .WithKey (jwa .HS256 , testStoreSvc . idTokenKey ))
426
+ signedIdToken , err := jwt .Sign (tok , jwt .WithKey (jwa .HS256 , testJwkKey ))
430
427
if err != nil {
431
428
panic (err )
432
429
}
@@ -449,7 +446,7 @@ func TestHandleOidcCallbackHappyPathWithPlatformInstanceID(t *testing.T) {
449
446
launchSvc := New (Config {
450
447
JWTKeySecret : testJWTSecret ,
451
448
Issuer : testIssuer ,
452
- }, testStoreSvc )
449
+ }, & mockStoreSvc {} )
453
450
454
451
state , err := createLaunchState (launchSvc .config .Issuer , launchSvc .config .JWTKeySecret , testLaunchID )
455
452
if err != nil {
@@ -475,7 +472,7 @@ func TestHandleOidcCallbackHappyPathWithPlatformInstanceID(t *testing.T) {
475
472
if err != nil {
476
473
panic (err )
477
474
}
478
- signedIdToken , err := jwt .Sign (tok , jwt .WithKey (jwa .HS256 , testStoreSvc . idTokenKey ))
475
+ signedIdToken , err := jwt .Sign (tok , jwt .WithKey (jwa .HS256 , testJwkKey ))
479
476
if err != nil {
480
477
panic (err )
481
478
}
@@ -498,7 +495,7 @@ func TestHandleOidcCallbackInvalidState(t *testing.T) {
498
495
launchSvc := New (Config {
499
496
JWTKeySecret : testJWTSecret ,
500
497
Issuer : testIssuer ,
501
- }, testStoreSvc )
498
+ }, & mockStoreSvc {} )
502
499
503
500
_ , err := launchSvc .HandleOidcCallback (context .Background (), peregrine.OIDCAuthenticationResponse {
504
501
State : "" ,
@@ -514,7 +511,7 @@ func TestHandleOidcCallbackInvalidIDToken(t *testing.T) {
514
511
launchSvc := New (Config {
515
512
JWTKeySecret : testJWTSecret ,
516
513
Issuer : testIssuer ,
517
- }, testStoreSvc )
514
+ }, & mockStoreSvc {} )
518
515
519
516
state , err := createLaunchState (launchSvc .config .Issuer , launchSvc .config .JWTKeySecret , testLaunchID )
520
517
if err != nil {
@@ -535,7 +532,7 @@ func TestHandleOidcCallbackLaunchIDNotFound(t *testing.T) {
535
532
launchSvc := New (Config {
536
533
JWTKeySecret : testJWTSecret ,
537
534
Issuer : testIssuer ,
538
- }, testStoreSvc )
535
+ }, & mockStoreSvc {} )
539
536
540
537
state , err := createLaunchState (launchSvc .config .Issuer , launchSvc .config .JWTKeySecret , testDeploymentID )
541
538
if err != nil {
@@ -558,7 +555,7 @@ func TestHandleOidcCallbackLaunchIDNotFound(t *testing.T) {
558
555
if err != nil {
559
556
panic (err )
560
557
}
561
- signedIdToken , err := jwt .Sign (tok , jwt .WithKey (jwa .HS256 , testStoreSvc . idTokenKey ))
558
+ signedIdToken , err := jwt .Sign (tok , jwt .WithKey (jwa .HS256 , testJwkKey ))
562
559
if err != nil {
563
560
panic (err )
564
561
}
@@ -601,7 +598,7 @@ func TestHandleOidcCallbackDeploymentUpsertFailure(t *testing.T) {
601
598
if err != nil {
602
599
panic (err )
603
600
}
604
- signedIdToken , err := jwt .Sign (tok , jwt .WithKey (jwa .HS256 , testStoreSvc . idTokenKey ))
601
+ signedIdToken , err := jwt .Sign (tok , jwt .WithKey (jwa .HS256 , testJwkKey ))
605
602
if err != nil {
606
603
panic (err )
607
604
}
@@ -643,7 +640,7 @@ func TestHandleOidcCallbackWithUpdateLaunchFailure(t *testing.T) {
643
640
if err != nil {
644
641
panic (err )
645
642
}
646
- signedIdToken , err := jwt .Sign (tok , jwt .WithKey (jwa .HS256 , testStoreSvc . idTokenKey ))
643
+ signedIdToken , err := jwt .Sign (tok , jwt .WithKey (jwa .HS256 , testJwkKey ))
647
644
if err != nil {
648
645
panic (err )
649
646
}
@@ -689,7 +686,7 @@ func TestHandleOidcCallbackWithUpsertPlatformInstanceFailure(t *testing.T) {
689
686
if err != nil {
690
687
panic (err )
691
688
}
692
- signedIdToken , err := jwt .Sign (tok , jwt .WithKey (jwa .HS256 , testStoreSvc . idTokenKey ))
689
+ signedIdToken , err := jwt .Sign (tok , jwt .WithKey (jwa .HS256 , testJwkKey ))
693
690
if err != nil {
694
691
panic (err )
695
692
}
@@ -709,7 +706,7 @@ func TestHandleOidcCallbackInvalidSubjectClaim(t *testing.T) {
709
706
launchSvc := New (Config {
710
707
JWTKeySecret : testJWTSecret ,
711
708
Issuer : testIssuer ,
712
- }, testStoreSvc )
709
+ }, & mockStoreSvc {} )
713
710
714
711
state , err := createLaunchState (launchSvc .config .Issuer , launchSvc .config .JWTKeySecret , testLaunchID )
715
712
if err != nil {
@@ -734,7 +731,7 @@ func TestHandleOidcCallbackInvalidSubjectClaim(t *testing.T) {
734
731
if err != nil {
735
732
panic (err )
736
733
}
737
- signedIdToken , err := jwt .Sign (tok , jwt .WithKey (jwa .HS256 , testStoreSvc . idTokenKey ))
734
+ signedIdToken , err := jwt .Sign (tok , jwt .WithKey (jwa .HS256 , testJwkKey ))
738
735
if err != nil {
739
736
panic (err )
740
737
}
@@ -749,11 +746,7 @@ func TestHandleOidcCallbackInvalidSubjectClaim(t *testing.T) {
749
746
}
750
747
751
748
// mockStoreSvc mocks the data store service dependency
752
- type mockStoreSvc struct {
753
- idTokenKey jwk.Key
754
- jwkServerURL string
755
- server * httptest.Server
756
- }
749
+ type mockStoreSvc struct {}
757
750
758
751
// UpsertPlatformInstanceByGUID should create a PlatformInstance if not existing returning PlatformInstance with ID
759
752
func (s * mockStoreSvc ) UpsertPlatformInstanceByGUID (ctx context.Context , instance peregrine.PlatformInstance ) (peregrine.PlatformInstance , error ) {
0 commit comments