You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
boundary/internal/servers/controller/handlers/outgoing_interceptor.go

41 lines
876 B

package handlers
import (
"context"
"net/http"
"strings"
pbs "github.com/hashicorp/boundary/internal/gen/controller/api/services"
"google.golang.org/protobuf/proto"
)
const (
HttpOnlyCookieName = "wt-http-token-cookie"
JsVisibleCookieName = "wt-js-token-cookie"
)
func OutgoingInterceptor(ctx context.Context, w http.ResponseWriter, m proto.Message) error {
m = m.ProtoReflect().Interface()
switch m := m.(type) {
case *pbs.AuthenticateResponse:
if strings.EqualFold(m.GetTokenType(), "cookie") {
tok := m.GetItem().GetToken()
m.GetItem().Token = ""
half := len(tok) / 2
jsTok := http.Cookie{
Name: JsVisibleCookieName,
Value: tok[:half],
}
httpTok := http.Cookie{
Name: HttpOnlyCookieName,
Value: tok[half:],
HttpOnly: true,
}
http.SetCookie(w, &jsTok)
http.SetCookie(w, &httpTok)
}
}
return nil
}