nezha/service/rpc/auth.go

61 lines
1.2 KiB
Go
Raw Normal View History

2019-12-08 16:59:58 +08:00
package rpc
2019-12-07 18:14:40 +08:00
import (
"context"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/metadata"
"google.golang.org/grpc/status"
)
// AuthHandler ..
type AuthHandler struct {
AppKey string
AppSecret string
}
// GetRequestMetadata ..
func (a *AuthHandler) GetRequestMetadata(ctx context.Context, uri ...string) (map[string]string, error) {
return map[string]string{"app_key": a.AppKey, "app_secret": a.AppSecret}, nil
}
// RequireTransportSecurity ..
func (a *AuthHandler) RequireTransportSecurity() bool {
return false
}
// Check ..
func (a *AuthHandler) Check(ctx context.Context) error {
md, ok := metadata.FromIncomingContext(ctx)
if !ok {
return status.Errorf(codes.Unauthenticated, "metadata.FromIncomingContext err")
}
var (
AppKey string
AppSecret string
)
if value, ok := md["app_key"]; ok {
AppKey = value[0]
}
if value, ok := md["app_secret"]; ok {
AppSecret = value[0]
}
if AppKey != a.GetAppKey() || AppSecret != a.GetAppSecret() {
return status.Errorf(codes.Unauthenticated, "invalid token")
}
return nil
}
// GetAppKey ..
func (a *AuthHandler) GetAppKey() string {
return a.AppKey
}
// GetAppSecret ..
func (a *AuthHandler) GetAppSecret() string {
return a.AppSecret
}