mirror of
https://github.com/Buriburizaem0n/nezha_domains.git
synced 2026-03-22 02:51:50 +00:00
Web 服务
This commit is contained in:
60
service/rpc/auth.go
Normal file
60
service/rpc/auth.go
Normal file
@@ -0,0 +1,60 @@
|
||||
package rpc
|
||||
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user