Skip to content

Commit

Permalink
Export functions (#8)
Browse files Browse the repository at this point in the history
* Export functions

* export withUser

* add depracted
  • Loading branch information
klaidliadon authored Oct 25, 2024
1 parent 733cff5 commit bbcdb51
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
24 changes: 16 additions & 8 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,10 @@ func GetSessionType(ctx context.Context) (proto.SessionType, bool) {
// Account
//

// withAccount adds the account to the context.
func withAccount(ctx context.Context, account string) context.Context {
// WithAccount adds the account to the context.
//
// Deprecated: this will be removed in the future, use Session middleware with a JWT token.
func WithAccount(ctx context.Context, account string) context.Context {
return context.WithValue(ctx, ctxKeyAccount, account)
}

Expand All @@ -60,8 +62,10 @@ func GetAccount(ctx context.Context) (string, bool) {
// User
//

// withUser adds the user to the context.
func withUser(ctx context.Context, user any) context.Context {
// WithUser adds the user to the context.
//
// Deprecated: this will be removed in the future, use Session middleware with a JWT token.
func WithUser(ctx context.Context, user any) context.Context {
return context.WithValue(ctx, ctxKeyUser, user)
}

Expand Down Expand Up @@ -90,8 +94,10 @@ func GetService(ctx context.Context) (string, bool) {
// AccessKey
//

// withAccessKey adds the access key to the context.
func withAccessKey(ctx context.Context, accessKey string) context.Context {
// WithAccessKey adds the access key to the context.
//
// Deprecated: this will be removed in the future, use Session middleware with a JWT token.
func WithAccessKey(ctx context.Context, accessKey string) context.Context {
return context.WithValue(ctx, ctxKeyAccessKey, accessKey)
}

Expand All @@ -105,8 +111,10 @@ func GetAccessKey(ctx context.Context) (string, bool) {
// Project ID
//

// withProjectID adds the project to the context.
func withProjectID(ctx context.Context, project uint64) context.Context {
// WithProjectID adds the project to the context.
//
// Deprecated: this will be removed in the future, use Session middleware with a JWT token.
func WithProjectID(ctx context.Context, project uint64) context.Context {
return context.WithValue(ctx, ctxKeyProjectID, project)
}

Expand Down
8 changes: 4 additions & 4 deletions middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ func Session(cfg *Options) func(next http.Handler) http.Handler {
ctx = WithService(ctx, serviceClaim)
sessionType = proto.SessionType_Service
case accountClaim != "":
ctx = withAccount(ctx, accountClaim)
ctx = WithAccount(ctx, accountClaim)
sessionType = proto.SessionType_Wallet

if cfg != nil && cfg.UserStore != nil {
Expand All @@ -90,7 +90,7 @@ func Session(cfg *Options) func(next http.Handler) http.Handler {
}

if user != nil {
ctx = withUser(ctx, user)
ctx = WithUser(ctx, user)

sessionType = proto.SessionType_User
if isAdmin {
Expand All @@ -105,14 +105,14 @@ func Session(cfg *Options) func(next http.Handler) http.Handler {

if projectClaim > 0 {
projectID := uint64(projectClaim)
ctx = withProjectID(ctx, projectID)
ctx = WithProjectID(ctx, projectID)
sessionType = proto.SessionType_Project
}
}
}

if accessKey != "" && sessionType < proto.SessionType_Admin {
ctx = withAccessKey(ctx, accessKey)
ctx = WithAccessKey(ctx, accessKey)
sessionType = max(sessionType, proto.SessionType_AccessKey)
}

Expand Down

0 comments on commit bbcdb51

Please sign in to comment.