mirror of
https://github.com/paralus/paralus.git
synced 2026-05-08 17:36:56 +00:00
fixes for cli auth
This commit is contained in:
@@ -63,6 +63,8 @@ func (ac authContext) NewAuthUnaryInterceptor(opt Option) grpc.UnaryServerInterc
|
||||
url string
|
||||
method string
|
||||
token string
|
||||
apiKey string
|
||||
apiTkn string
|
||||
cookie string
|
||||
host string
|
||||
ua string
|
||||
@@ -77,6 +79,12 @@ func (ac authContext) NewAuthUnaryInterceptor(opt Option) grpc.UnaryServerInterc
|
||||
if len(md.Get(gateway.GatewayAPIKey)) != 0 {
|
||||
token = md.Get(gateway.GatewayAPIKey)[0]
|
||||
}
|
||||
if len(md.Get("X-API-KEYID")) != 0 {
|
||||
apiKey = md.Get("X-API-KEYID")[0]
|
||||
}
|
||||
if len(md.Get("X-API-TOKEN")) != 0 {
|
||||
apiTkn = md.Get("X-API-TOKEN")[0]
|
||||
}
|
||||
if len(md.Get("grpcgateway-cookie")) != 0 {
|
||||
cookie = md.Get("grpcgateway-cookie")[0]
|
||||
}
|
||||
@@ -94,11 +102,14 @@ func (ac authContext) NewAuthUnaryInterceptor(opt Option) grpc.UnaryServerInterc
|
||||
Url: url,
|
||||
Method: method,
|
||||
XSessionToken: token,
|
||||
XApiKey: apiKey,
|
||||
XApiToken: apiTkn,
|
||||
Cookie: cookie,
|
||||
Org: org,
|
||||
Project: project,
|
||||
NoAuthz: noAuthz, // FIXME: any better way to do this?
|
||||
}
|
||||
|
||||
res, err := ac.IsRequestAllowed(ctx, nil, acReq)
|
||||
if err != nil {
|
||||
_log.Errorf("Failed to authenticate a request: %s", err)
|
||||
|
||||
@@ -77,7 +77,8 @@ func (am *authMiddleware) ServeHTTP(rw http.ResponseWriter, r *http.Request, nex
|
||||
Url: r.URL.String(),
|
||||
Method: r.Method,
|
||||
XSessionToken: r.Header.Get("X-Session-Token"),
|
||||
XApiKey: r.Header.Get("X-RAFAY-API-KEYID"),
|
||||
XApiKey: r.Header.Get("X-API-KEYID"),
|
||||
XApiToken: r.Header.Get("X-API-TOKEN"),
|
||||
Cookie: r.Header.Get("Cookie"),
|
||||
Project: poResp.Project,
|
||||
Org: poResp.Organization,
|
||||
|
||||
@@ -2,6 +2,8 @@ package authv3
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/md5"
|
||||
"encoding/base64"
|
||||
"errors"
|
||||
"net/http"
|
||||
"strings"
|
||||
@@ -9,7 +11,6 @@ import (
|
||||
rpcv3 "github.com/RafayLabs/rcloud-base/proto/rpc/user"
|
||||
authzv1 "github.com/RafayLabs/rcloud-base/proto/types/authz"
|
||||
commonv3 "github.com/RafayLabs/rcloud-base/proto/types/commonpb/v3"
|
||||
"github.com/spacemonkeygo/httpsig"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -48,6 +49,12 @@ func (ac *authContext) IsRequestAllowed(ctx context.Context, httpreq *http.Reque
|
||||
return res, nil
|
||||
}
|
||||
|
||||
func getTokenCheckSum(body []byte) string {
|
||||
hash := md5.New()
|
||||
hash.Write(body)
|
||||
return base64.StdEncoding.EncodeToString(hash.Sum(nil))
|
||||
}
|
||||
|
||||
// authenticate validate whether the request is from a legitimate user
|
||||
// and populate relevant information in res.
|
||||
func (ac *authContext) authenticate(ctx context.Context, httpreq *http.Request, req *commonv3.IsRequestAllowedRequest, res *commonv3.IsRequestAllowedResponse) (bool, error) {
|
||||
@@ -59,16 +66,10 @@ func (ac *authContext) authenticate(ctx context.Context, httpreq *http.Request,
|
||||
_log.Infow("unable to get api key", "key", req.XApiKey, "error", err)
|
||||
return false, ErrInvalidAPIKey
|
||||
}
|
||||
var kg httpsig.KeyGetterFunc = func(id string) interface{} {
|
||||
return []byte(resp.Secret)
|
||||
}
|
||||
|
||||
verifier := httpsig.NewVerifier(kg)
|
||||
verifier.SetRequiredHeaders([]string{"content-md5", "date", "host", "nonce"})
|
||||
err = verifier.Verify(httpreq)
|
||||
if err != nil {
|
||||
if !(req.XApiToken == getTokenCheckSum([]byte(resp.Secret))) {
|
||||
return false, ErrInvalidSignature
|
||||
}
|
||||
_log.Info("successfully validated api key ", req.XApiKey)
|
||||
res.Status = commonv3.RequestStatus_RequestAllowed
|
||||
res.SessionData.Username = resp.Name
|
||||
res.SessionData.Account = resp.AccountID.String()
|
||||
|
||||
Reference in New Issue
Block a user