wip: mock authorization server

This commit is contained in:
Kim Tore Jensen
2021-08-23 15:12:47 +02:00
parent 764adc3d77
commit 9354ee7629
2 changed files with 44 additions and 4 deletions

View File

@@ -0,0 +1,38 @@
package router_test
import (
"encoding/json"
"github.com/go-chi/chi"
"net/http"
)
type idporten struct {
}
type TokenJSON struct {
AccessToken string `json:"access_token"`
TokenType string `json:"token_type"`
RefreshToken string `json:"refresh_token"`
ExpiresIn int32 `json:"expires_in"`
IDToken string `json:"id_token"`
}
func (ip *idporten) Authorize(w http.ResponseWriter, r *http.Request) {
// fixme: generate valid access token and id token; sign them with the correct key
token := &TokenJSON{
AccessToken: "access-token",
TokenType: "token-type",
RefreshToken: "refresh-token",
IDToken: "id-token",
ExpiresIn: 1200,
}
w.Header().Set("content-type", "application/json")
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(token)
}
func idportenRouter(ip *idporten) chi.Router {
r := chi.NewRouter()
r.Post("/authorize", ip.Authorize)
return r
}

View File

@@ -3,7 +3,6 @@ package router_test
import (
"encoding/base64"
"github.com/nais/wonderwall/pkg/cryptutil"
"golang.org/x/net/publicsuffix"
"golang.org/x/oauth2"
"net/http"
"net/http/cookiejar"
@@ -105,9 +104,12 @@ func TestHandler_Callback(t *testing.T) {
r := router.New(h)
server := httptest.NewServer(r)
jar, err := cookiejar.New(&cookiejar.Options{
PublicSuffixList: publicsuffix.List,
})
idp := &idporten{}
idprouter := idportenRouter(idp)
idpserver := httptest.NewServer(idprouter)
h.OauthConfig.Endpoint.TokenURL = idpserver.URL + "/authorize"
jar, err := cookiejar.New(nil)
assert.NoError(t, err)
client := server.Client()