MCPcopy Create free account
hub / github.com/couchbase/cbft / tryBasicAuth

Function tryBasicAuth

grpc_auth.go:42–86  ·  view source on GitHub ↗
(req interface{}, ctx context.Context,
	rpcPath string)

Source from the content-addressed store, hash-verified

40}
41
42func tryBasicAuth(req interface{}, ctx context.Context,
43 rpcPath string) (context.Context, error) {
44 srv := req.(*SearchService)
45 if srv == nil {
46 return nil, fmt.Errorf("invalid request type")
47 }
48
49 auth, err := extractMetaHeader(ctx, "authorization")
50 if err != nil {
51 return ctx, status.Errorf(codes.Unauthenticated,
52 "err: %v", err)
53 }
54
55 const prefix = "Basic "
56 if !strings.HasPrefix(auth, prefix) {
57 return ctx, status.Error(codes.Unauthenticated,
58 `missing "Basic " prefix in "Authorization" header`)
59 }
60
61 c, err := base64.StdEncoding.DecodeString(auth[len(prefix):])
62 if err != nil {
63 return ctx, status.Error(codes.Unauthenticated,
64 `invalid base64 in header`)
65 }
66
67 cs := string(c)
68 s := strings.IndexByte(cs, ':')
69 if s < 0 {
70 return ctx, status.Error(codes.Unauthenticated,
71 `invalid basic auth format`)
72 }
73 user, passwd := cs[:s], cs[s+1:]
74 creds, err := cbauth.Auth(user, passwd)
75 if err != nil {
76 return nil, err
77 }
78
79 var authFunc gRPCAuthHandler
80 aw := &authWrapper{mgr: srv.mgr, creds: creds,
81 path: rpcPath[strings.LastIndex(rpcPath, "/"):], method: "RPC"}
82 authFunc = aw.authenticate
83
84 nctx := context.WithValue(ctx, gRPCAuthHandlerKey, authFunc)
85 return nctx, nil
86}
87
88func extractMetaHeader(ctx context.Context, header string) (string, error) {
89 md, ok := metadata.FromIncomingContext(ctx)

Callers 1

wrapAuthCallbacksFunction · 0.85

Calls 1

extractMetaHeaderFunction · 0.85

Tested by

no test coverage detected