Skip to content

Commit c966608

Browse files
authored
Consider AuthInfo as optional in check (#53)
* 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
1 parent 726365c commit c966608

5 files changed

Lines changed: 15 additions & 11 deletions

File tree

pkg/authz/check/handlers.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ func (svc CheckService) GetRoutes() []service.Route {
2525

2626
func authorize(env service.Env, w http.ResponseWriter, r *http.Request) error {
2727
authInfo := service.GetAuthInfoFromRequestContext(r.Context())
28-
29-
if authInfo.UserId != "" {
28+
if authInfo != nil && authInfo.UserId != "" {
3029
var sessionCheckManySpec SessionCheckManySpec
3130
err := service.ParseJSONBody(r.Body, &sessionCheckManySpec)
3231
if err != nil {
@@ -54,7 +53,7 @@ func authorize(env service.Env, w http.ResponseWriter, r *http.Request) error {
5453
Debug: sessionCheckManySpec.Debug,
5554
}
5655

57-
checkResult, err := NewService(env, &authInfo).CheckMany(r.Context(), &checkManySpec)
56+
checkResult, err := NewService(env, authInfo).CheckMany(r.Context(), &checkManySpec)
5857
if err != nil {
5958
return err
6059
}
@@ -69,7 +68,7 @@ func authorize(env service.Env, w http.ResponseWriter, r *http.Request) error {
6968
return err
7069
}
7170

72-
checkResult, err := NewService(env, &authInfo).CheckMany(r.Context(), &checkManySpec)
71+
checkResult, err := NewService(env, authInfo).CheckMany(r.Context(), &checkManySpec)
7372
if err != nil {
7473
return err
7574
}

pkg/authz/check/service.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ func (svc CheckService) getMatchingSubjects(ctx context.Context, objectType stri
7777
wntCtx.ToHash(),
7878
)
7979
if err != nil {
80-
log.Warn().Err(err).Msg("Error fetching warrants for object")
80+
log.Err(err).Msg("Error fetching warrants for object")
8181
return warrantSpecs, err
8282
}
8383

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

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

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

pkg/authz/object/mysql.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ func (repo MySQLRepository) Create(ctx context.Context, object Object) (int64, e
4444

4545
newObjectId, err := result.LastInsertId()
4646
if err != nil {
47-
log.Warn().Err(err).Msg("Unable to create object")
47+
log.Err(err).Msg("Unable to create object")
4848
return 0, service.NewInternalError("Unable to create object")
4949
}
5050

pkg/authz/warrant/spec.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,6 @@ type WarrantSpec struct {
9595
Relation string `json:"relation" validate:"required,valid_relation"`
9696
Subject *SubjectSpec `json:"subject" validate:"required"`
9797
Context context.ContextSetSpec `json:"context,omitempty"`
98-
IsImplicit *bool `json:"isImplicit,omitempty"`
9998
CreatedAt time.Time `json:"createdAt"`
10099
}
101100

pkg/service/auth.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,12 @@ func AuthMiddleware(next http.Handler, config *config.Config, enableSessionAuth
165165
}
166166

167167
// GetAuthInfoFromRequestContext returns the AuthInfo object from the given context
168-
func GetAuthInfoFromRequestContext(context context.Context) AuthInfo {
169-
return context.Value(authInfoKey).(AuthInfo)
168+
func GetAuthInfoFromRequestContext(context context.Context) *AuthInfo {
169+
contextVal := context.Value(authInfoKey)
170+
if contextVal != nil {
171+
authInfo := context.Value(authInfoKey).(AuthInfo)
172+
return &authInfo
173+
}
174+
175+
return nil
170176
}

0 commit comments

Comments
 (0)