Skip to content

Commit

Permalink
Consider AuthInfo as optional in check (warrant-dev#53)
Browse files Browse the repository at this point in the history
* Run go mod tidy

* Update GetAuthInfoFromRequestContext to handle cases when AuthInfo doesn't exist

* Update check handlers and service to work with or without AuthInfo

* Remove logger Warn call on some errors

* Remove unused IsImplicit attribute on WarrantSpec

* Remove participle as a dependency
  • Loading branch information
kkajla12 authored Apr 3, 2023
1 parent 726365c commit c966608
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 11 deletions.
7 changes: 3 additions & 4 deletions pkg/authz/check/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ func (svc CheckService) GetRoutes() []service.Route {

func authorize(env service.Env, w http.ResponseWriter, r *http.Request) error {
authInfo := service.GetAuthInfoFromRequestContext(r.Context())

if authInfo.UserId != "" {
if authInfo != nil && authInfo.UserId != "" {
var sessionCheckManySpec SessionCheckManySpec
err := service.ParseJSONBody(r.Body, &sessionCheckManySpec)
if err != nil {
Expand Down Expand Up @@ -54,7 +53,7 @@ func authorize(env service.Env, w http.ResponseWriter, r *http.Request) error {
Debug: sessionCheckManySpec.Debug,
}

checkResult, err := NewService(env, &authInfo).CheckMany(r.Context(), &checkManySpec)
checkResult, err := NewService(env, authInfo).CheckMany(r.Context(), &checkManySpec)
if err != nil {
return err
}
Expand All @@ -69,7 +68,7 @@ func authorize(env service.Env, w http.ResponseWriter, r *http.Request) error {
return err
}

checkResult, err := NewService(env, &authInfo).CheckMany(r.Context(), &checkManySpec)
checkResult, err := NewService(env, authInfo).CheckMany(r.Context(), &checkManySpec)
if err != nil {
return err
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/authz/check/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func (svc CheckService) getMatchingSubjects(ctx context.Context, objectType stri
wntCtx.ToHash(),
)
if err != nil {
log.Warn().Err(err).Msg("Error fetching warrants for object")
log.Err(err).Msg("Error fetching warrants for object")
return warrantSpecs, err
}

Expand All @@ -97,7 +97,7 @@ func (svc CheckService) getMatchingSubjects(ctx context.Context, objectType stri
wntCtx.ToHash(),
)
if err != nil {
log.Warn().Err(err).Msg("Error fetching warrants matching wildcard")
log.Err(err).Msg("Error fetching warrants matching wildcard")
return warrantSpecs, err
}

Expand Down Expand Up @@ -326,7 +326,7 @@ func (svc CheckService) Check(ctx context.Context, warrantCheck CheckSpec) (matc
log.Debug().Msgf("Checking for warrant %s", warrantCheck.String())

// Used to automatically append tenant context for session token w/ tenantId checks
if svc.authInfo.TenantId != "" {
if svc.authInfo != nil && svc.authInfo.TenantId != "" {
svc.appendTenantContext(&warrantCheck)
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/authz/object/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func (repo MySQLRepository) Create(ctx context.Context, object Object) (int64, e

newObjectId, err := result.LastInsertId()
if err != nil {
log.Warn().Err(err).Msg("Unable to create object")
log.Err(err).Msg("Unable to create object")
return 0, service.NewInternalError("Unable to create object")
}

Expand Down
1 change: 0 additions & 1 deletion pkg/authz/warrant/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ type WarrantSpec struct {
Relation string `json:"relation" validate:"required,valid_relation"`
Subject *SubjectSpec `json:"subject" validate:"required"`
Context context.ContextSetSpec `json:"context,omitempty"`
IsImplicit *bool `json:"isImplicit,omitempty"`
CreatedAt time.Time `json:"createdAt"`
}

Expand Down
10 changes: 8 additions & 2 deletions pkg/service/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,12 @@ func AuthMiddleware(next http.Handler, config *config.Config, enableSessionAuth
}

// GetAuthInfoFromRequestContext returns the AuthInfo object from the given context
func GetAuthInfoFromRequestContext(context context.Context) AuthInfo {
return context.Value(authInfoKey).(AuthInfo)
func GetAuthInfoFromRequestContext(context context.Context) *AuthInfo {
contextVal := context.Value(authInfoKey)
if contextVal != nil {
authInfo := context.Value(authInfoKey).(AuthInfo)
return &authInfo
}

return nil
}

0 comments on commit c966608

Please sign in to comment.