11package authn
22
33import (
4- "encoding/base64"
54 "encoding/json"
65 "io"
76 "net/http"
8- "net/url"
97 "strconv"
10- "strings"
118 "testing"
129
1310 "github.com/nuts-foundation/nuts-knooppunt/cmd/core"
1411 httpComponent "github.com/nuts-foundation/nuts-knooppunt/component/http"
15- "github.com/nuts-foundation/nuts-knooppunt/lib/from"
1612 "github.com/nuts-foundation/nuts-knooppunt/lib/netutil"
1713 "github.com/stretchr/testify/require"
1814)
@@ -24,10 +20,10 @@ func Test_RequestToken(t *testing.T) {
2420 publicMux := http .NewServeMux ()
2521 httpConfig := httpComponent .DefaultConfig ()
2622 httpConfig .InternalInterface = httpComponent.InterfaceConfig {
27- Listener : ":" + strconv .Itoa (p1 ),
28- BaseURL : "http://localhost:" + strconv .Itoa (p1 ),
23+ Address : ":" + strconv .Itoa (p1 ),
24+ BaseURL : "http://localhost:" + strconv .Itoa (p1 ),
2925 }
30- httpConfig .PublicInterface .Listener = ":" + strconv .Itoa (p2 )
26+ httpConfig .PublicInterface .Address = ":" + strconv .Itoa (p2 )
3127 httpService := httpComponent .New (httpConfig , publicMux , internalMux )
3228
3329 config := Config {
@@ -58,71 +54,39 @@ func Test_RequestToken(t *testing.T) {
5854 require .Equal (t , data ["token_endpoint" ], httpService .Internal ().URL ().JoinPath ("/auth/token" ).String ())
5955 require .Equal (t , data ["issuer" ], httpService .Public ().URL ().JoinPath ("/auth" ).String ())
6056 })
61- t .Run ("Token Exchange grant type" , func (t * testing.T ) {
62- params , _ := json .Marshal (map [string ][]string {
63- "grant_type" : {"urn:ietf:params:oauth:grant-type:token-exchange" },
64- "client_id" : {"test-client" },
65- "client_secret" : {"test-secret" },
66- "subject_token" : {"TODO(subject token)" },
67- "subject_token_type" : {"urn:ietf:params:oauth:token-type:id_token" },
68- "actor_token" : {"TODO(actor token)" },
69- "actor_token_type" : {"nuts-subject-id" },
70- "audience" : {"TODO(audience)" },
71- "scope" : {"some-scope" },
72- })
73- http .NewRequest (http .MethodPost , httpService .Internal ().URL ().JoinPath ("/auth/token" ).String (),
74- httpResponse , err := http .PostForm (httpService .Internal ().URL ().JoinPath ("/auth/token" ).String (), map [string ][]string {
75- "grant_type" : {"urn:ietf:params:oauth:grant-type:token-exchange" },
76- "client_id" : {"test-client" },
77- "client_secret" : {"test-secret" },
78- "subject_token" : {"TODO(subject token)" },
79- "subject_token_type" : {"urn:ietf:params:oauth:token-type:id_token" },
80- "actor_token" : {"TODO(actor token)" },
81- "actor_token_type" : {"nuts-subject-id" },
82- "audience" : {"TODO(audience)" },
83- "scope" : {"some-scope" },
84- })
85- require .NoError (t , err )
86- defer httpResponse .Body .Close ()
87-
88- data , err := from.JSONResponse [map [string ]any ](httpResponse )
89- require .NoError (t , err )
90-
91- require .NotEmpty (t , data ["access_token" ])
92- })
93- t .Run ("Client Credentials grant type" , func (t * testing.T ) {
94- httpResponse , err := http .PostForm (httpService .Internal ().URL ().JoinPath ("/auth/token" ).String (), map [string ][]string {
95- "grant_type" : {"client_credentials" },
96- "client_id" : {"test-client" },
97- "client_secret" : {"test-secret" },
98- "scope" : {"openid" },
99- })
100- require .NoError (t , err )
101- defer httpResponse .Body .Close ()
102- data , err := from.JSONResponse [map [string ]any ](httpResponse )
103- require .NoError (t , err )
104-
105- require .NotEmpty (t , data ["access_token" ])
106- require .NotEmpty (t , data ["expires_in" ])
107- require .Equal (t , data ["token_type" ], "Bearer" )
108- require .Equal (t , data ["scope" ], "openid" )
109-
110- t .Run ("introspect token" , func (t * testing.T ) {
111- httpRequest , _ := http .NewRequest (http .MethodPost , httpService .Internal ().URL ().JoinPath ("/auth/introspect" ).String (), strings .NewReader (url.Values {
112- "token" : {data ["access_token" ].(string )},
113- }.Encode ()))
114- httpRequest .Header .Set ("Content-Type" , "application/x-www-form-urlencoded" )
115- httpRequest .Header .Set ("Authorization" , "Basic " + base64 .StdEncoding .EncodeToString ([]byte ("test-client:test-secret" )))
116-
117- httpResponse , err := http .DefaultClient .Do (httpRequest )
118- require .NoError (t , err )
119- defer httpResponse .Body .Close ()
120- response , err := from.JSONResponse [map [string ]any ](httpResponse )
121-
122- require .NoError (t , err )
123- require .Equal (t , true , response ["active" ])
124- require .Equal (t , "openid" , response ["scope" ])
125- require .Equal (t , []interface {}{"TODO(audience)" }, response ["aud" ])
126- })
127- })
57+ //t.Run("Client Credentials grant type", func(t *testing.T) {
58+ // httpResponse, err := http.PostForm(httpService.Internal().URL().JoinPath("/auth/token").String(), map[string][]string{
59+ // "grant_type": {"client_credentials"},
60+ // "client_id": {"test-client"},
61+ // "client_secret": {"test-secret"},
62+ // "scope": {"openid"},
63+ // })
64+ // require.NoError(t, err)
65+ // defer httpResponse.Body.Close()
66+ // data, err := from.JSONResponse[map[string]any](httpResponse)
67+ // require.NoError(t, err)
68+ //
69+ // require.NotEmpty(t, data["access_token"])
70+ // require.NotEmpty(t, data["expires_in"])
71+ // require.Equal(t, data["token_type"], "Bearer")
72+ // require.Equal(t, data["scope"], "openid")
73+ //
74+ // t.Run("introspect token", func(t *testing.T) {
75+ // httpRequest, _ := http.NewRequest(http.MethodPost, httpService.Internal().URL().JoinPath("/auth/introspect").String(), strings.NewReader(url.Values{
76+ // "token": {data["access_token"].(string)},
77+ // }.Encode()))
78+ // httpRequest.Header.Set("Content-Type", "application/x-www-form-urlencoded")
79+ // httpRequest.Header.Set("Authorization", "Basic "+base64.StdEncoding.EncodeToString([]byte("test-client:test-secret")))
80+ //
81+ // httpResponse, err := http.DefaultClient.Do(httpRequest)
82+ // require.NoError(t, err)
83+ // defer httpResponse.Body.Close()
84+ // response, err := from.JSONResponse[map[string]any](httpResponse)
85+ //
86+ // require.NoError(t, err)
87+ // require.Equal(t, true, response["active"])
88+ // require.Equal(t, "openid", response["scope"])
89+ // require.Equal(t, []interface{}{"TODO(audience)"}, response["aud"])
90+ // })
91+ //})
12892}
0 commit comments