1 Star 0 Fork 0

jerryduren / architecture

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
http.go 2.01 KB
一键复制 编辑 原始数据 按行查看 历史
jerryduren 提交于 2021-09-21 23:31 . add
package auth
import (
"context"
"net/http"
"strings"
"firebase.google.com/go/auth"
commonerrors "github.com/ThreeDotsLabs/wild-workouts-go-ddd-example/internal/common/errors"
"github.com/ThreeDotsLabs/wild-workouts-go-ddd-example/internal/common/server/httperr"
)
type FirebaseHttpMiddleware struct {
AuthClient *auth.Client
}
func (a FirebaseHttpMiddleware) Middleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
bearerToken := a.tokenFromHeader(r)
if bearerToken == "" {
httperr.Unauthorised("empty-bearer-token", nil, w, r)
return
}
token, err := a.AuthClient.VerifyIDToken(ctx, bearerToken)
if err != nil {
httperr.Unauthorised("unable-to-verify-jwt", err, w, r)
return
}
// it's always a good idea to use custom type as context value (in this case ctxKey)
// because nobody from the outside of the package will be able to override/read this value
ctx = context.WithValue(ctx, userContextKey, User{
UUID: token.UID,
Email: token.Claims["email"].(string),
Role: token.Claims["role"].(string),
DisplayName: token.Claims["name"].(string),
})
r = r.WithContext(ctx)
next.ServeHTTP(w, r)
})
}
func (a FirebaseHttpMiddleware) tokenFromHeader(r *http.Request) string {
headerValue := r.Header.Get("Authorization")
if len(headerValue) > 7 && strings.ToLower(headerValue[0:6]) == "bearer" {
return headerValue[7:]
}
return ""
}
type User struct {
UUID string
Email string
Role string
DisplayName string
}
type ctxKey int
const (
userContextKey ctxKey = iota
)
var (
// if we expect that the user of the function may be interested with concrete error,
// it's a good idea to provide variable with this error
NoUserInContextError = commonerrors.NewAuthorizationError("no user in context", "no-user-found")
)
func UserFromCtx(ctx context.Context) (User, error) {
u, ok := ctx.Value(userContextKey).(User)
if ok {
return u, nil
}
return User{}, NoUserInContextError
}
Go
1
https://gitee.com/jerryduren/architecture.git
git@gitee.com:jerryduren/architecture.git
jerryduren
architecture
architecture
36a2cef21a1f

搜索帮助

53164aa7 5694891 3bd8fe86 5694891