mirror of
https://github.com/aljazceru/ark.git
synced 2025-12-18 20:54:20 +01:00
Add AdminService (#176)
* add admin service * go mod tidy * fix linter: grpc.Dial * fix ocean get balance * fix linter * add .vscode to gitignore * rework admin balance API * fix mockedwallet in covenantless pkg * make proto
This commit is contained in:
42
server/internal/interface/grpc/interceptors/auth.go
Normal file
42
server/internal/interface/grpc/interceptors/auth.go
Normal file
@@ -0,0 +1,42 @@
|
||||
package interceptors
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/base64"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
arkv1 "github.com/ark-network/ark/api-spec/protobuf/gen/ark/v1"
|
||||
grpc_auth "github.com/grpc-ecosystem/go-grpc-middleware/auth"
|
||||
"google.golang.org/grpc"
|
||||
"google.golang.org/grpc/codes"
|
||||
"google.golang.org/grpc/status"
|
||||
)
|
||||
|
||||
func unaryAuthenticator(user, pass string) grpc.UnaryServerInterceptor {
|
||||
adminToken := fmt.Sprintf("%s:%s", user, pass)
|
||||
adminTokenEncoded := base64.StdEncoding.EncodeToString([]byte(adminToken))
|
||||
|
||||
return func(
|
||||
ctx context.Context,
|
||||
req interface{},
|
||||
info *grpc.UnaryServerInfo,
|
||||
handler grpc.UnaryHandler,
|
||||
) (interface{}, error) {
|
||||
// whitelist the ArkService
|
||||
if strings.Contains(info.FullMethod, arkv1.ArkService_ServiceDesc.ServiceName) {
|
||||
return handler(ctx, req)
|
||||
}
|
||||
|
||||
token, err := grpc_auth.AuthFromMD(ctx, "basic")
|
||||
if err != nil {
|
||||
return nil, status.Errorf(codes.Unauthenticated, "no basic header found: %v", err)
|
||||
}
|
||||
|
||||
if token != adminTokenEncoded {
|
||||
return nil, status.Errorf(codes.Unauthenticated, "invalid auth credentials: %v", err)
|
||||
}
|
||||
|
||||
return handler(ctx, req)
|
||||
}
|
||||
}
|
||||
@@ -6,8 +6,11 @@ import (
|
||||
)
|
||||
|
||||
// UnaryInterceptor returns the unary interceptor
|
||||
func UnaryInterceptor() grpc.ServerOption {
|
||||
return grpc.UnaryInterceptor(middleware.ChainUnaryServer(unaryLogger))
|
||||
func UnaryInterceptor(user, pass string) grpc.ServerOption {
|
||||
return grpc.UnaryInterceptor(middleware.ChainUnaryServer(
|
||||
unaryAuthenticator(user, pass),
|
||||
unaryLogger,
|
||||
))
|
||||
}
|
||||
|
||||
// StreamInterceptor returns the stream interceptor with a logrus log
|
||||
|
||||
Reference in New Issue
Block a user