48 lines
1.5 KiB
Go
48 lines
1.5 KiB
Go
package fbhttp
|
|
|
|
import (
|
|
"context"
|
|
"encoding/base64"
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
)
|
|
|
|
// Filesystem regression tests exercise authorization through the same identity
|
|
// context as the SSO middleware, without accepting test credentials in production.
|
|
func testSession(id uint, name string, admin bool) string {
|
|
b, _ := json.Marshal(sessionIdentity{ID: id, Name: name, Admin: admin})
|
|
return base64.RawURLEncoding.EncodeToString(b)
|
|
}
|
|
func authorizeTestRequest(r *http.Request, token string) *http.Request {
|
|
r.Header.Set("X-Test-Identity", token)
|
|
var i sessionIdentity
|
|
b, _ := base64.RawURLEncoding.DecodeString(token)
|
|
_ = json.Unmarshal(b, &i)
|
|
return r.WithContext(context.WithValue(r.Context(), identityKey{}, i))
|
|
}
|
|
|
|
func TestSSORoles(t *testing.T) {
|
|
for _, tc := range []struct {
|
|
roles []string
|
|
admin bool
|
|
}{{[]string{"Master Admin"}, true}, {[]string{"FS Admin"}, true}, {[]string{"User", "SSO Admin"}, false}, {[]string{"master admin"}, false}, {nil, false}} {
|
|
if ssoAdmin(tc.roles) != tc.admin {
|
|
t.Errorf("unexpected access for %v", tc.roles)
|
|
}
|
|
}
|
|
}
|
|
func TestLegacyTokenCannotAuthenticate(t *testing.T) {
|
|
r := httptest.NewRequest("GET", "/", nil)
|
|
r.Header.Set("X-Auth", "old.jwt.token")
|
|
r.AddCookie(&http.Cookie{Name: "auth", Value: "old.jwt.token"})
|
|
code, _ := withUser(func(http.ResponseWriter, *http.Request, *data) (int, error) {
|
|
t.Fatal("legacy credential accepted")
|
|
return 0, nil
|
|
})(httptest.NewRecorder(), r, &data{})
|
|
if code != 401 {
|
|
t.Fatalf("got %d", code)
|
|
}
|
|
}
|