46 lines
1.3 KiB
Go
46 lines
1.3 KiB
Go
package handler
|
|
|
|
import (
|
|
"crypto/hmac"
|
|
"crypto/sha256"
|
|
"encoding/base64"
|
|
"encoding/json"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/wmms/wmms/internal/service"
|
|
)
|
|
|
|
// A DATETIME written in the driver's default UTC location must be compared to
|
|
// a bound time in that same location, rather than the database's local NOW().
|
|
func TestSessionRevocationReloadAcrossTimeZonesMySQL(t *testing.T) {
|
|
f := newCompanyHTTPFixture(t)
|
|
f.db.SetMaxOpenConns(1)
|
|
companySQL(t, f.db, `SET time_zone = '+08:00'`)
|
|
parts := strings.Split(f.tokens["beijing"], ".")
|
|
payload, err := base64.RawURLEncoding.DecodeString(parts[1])
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
var principal service.SimpleToken
|
|
if err := json.Unmarshal(payload, &principal); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
principal.Exp = time.Now().Add(time.Hour).Unix()
|
|
payload, err = json.Marshal(principal)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
parts[1] = base64.RawURLEncoding.EncodeToString(payload)
|
|
mac := hmac.New(sha256.New, []byte("warehouse-mms-secret-key"))
|
|
mac.Write([]byte(parts[0] + "." + parts[1]))
|
|
parts[2] = base64.RawURLEncoding.EncodeToString(mac.Sum(nil))
|
|
f.tokens["beijing"] = strings.Join(parts, ".")
|
|
f.request(t, "beijing", "", "POST", "/api/auth/logout", "", 200)
|
|
if err := service.LoadRevokedSessions(); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
f.request(t, "beijing", "301", "GET", "/api/materials", "", 401)
|
|
}
|