Separate authn flow from analytics (#68327)

* separate authn flow from analytics

* lint fix
This commit is contained in:
Jo
2023-05-15 18:38:54 +02:00
committed by GitHub
parent ab5a3820d5
commit 5ec0f82baa
9 changed files with 169 additions and 135 deletions
+1 -23
View File
@@ -3,9 +3,6 @@ package contexthandler
import (
"context"
"crypto/hmac"
"crypto/sha256"
"encoding/hex"
"errors"
"fmt"
"net/http"
@@ -113,25 +110,6 @@ func FromContext(c context.Context) *contextmodel.ReqContext {
return nil
}
func hashUserIdentifier(identifier string, secret string) string {
key := []byte(secret)
h := hmac.New(sha256.New, key)
h.Write([]byte(identifier))
return hex.EncodeToString(h.Sum(nil))
}
func setSignedInUser(reqContext *contextmodel.ReqContext, identity *authn.Identity, intercomSecret string) {
reqContext.SignedInUser = identity.SignedInUser()
if identity.AuthID != "" {
reqContext.SignedInUser.Analytics.Identifier = identity.AuthID
} else {
reqContext.SignedInUser.Analytics.Identifier = identity.Email + "@" + setting.AppUrl
}
if intercomSecret != "" {
reqContext.SignedInUser.Analytics.IntercomIdentifier = hashUserIdentifier(identity.AuthID, intercomSecret)
}
}
// Middleware provides a middleware to initialize the request context.
func (h *ContextHandler) Middleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
@@ -172,8 +150,8 @@ func (h *ContextHandler) Middleware(next http.Handler) http.Handler {
// Hack: set all errors on LookupTokenErr, so we can check it in auth middlewares
reqContext.LookupTokenErr = err
} else {
reqContext.SignedInUser = identity.SignedInUser()
reqContext.UserToken = identity.SessionToken
setSignedInUser(reqContext, identity, h.Cfg.IntercomSecret)
reqContext.IsSignedIn = !identity.IsAnonymous
reqContext.AllowAnonymous = identity.IsAnonymous
reqContext.IsRenderCall = identity.AuthModule == login.RenderModule