-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
339 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package auth | ||
|
||
import ( | ||
"context" | ||
) | ||
|
||
// ctxKey represents the type of value for the context key. | ||
type ctxKey int | ||
|
||
// key is used to store/retrieve a Claims value from a context.Context. | ||
const claimKey ctxKey = 1 | ||
|
||
// ============================================================================= | ||
|
||
// SetClaims stores the claims in the context. | ||
func SetClaims(ctx context.Context, claims Claims) context.Context { | ||
return context.WithValue(ctx, claimKey, claims) | ||
} | ||
|
||
// GetClaims returns the claims from the context. | ||
func GetClaims(ctx context.Context) Claims { | ||
v, ok := ctx.Value(claimKey).(Claims) | ||
if !ok { | ||
return Claims{} | ||
} | ||
return v | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package auth | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
) | ||
|
||
// AuthError is used to pass an error during the request through the | ||
// application with auth specific context. | ||
type AuthError struct { | ||
msg string | ||
} | ||
|
||
// NewAuthError creates an AuthError for the provided message. | ||
func NewAuthError(format string, args ...any) error { | ||
return &AuthError{ | ||
msg: fmt.Sprintf(format, args...), | ||
} | ||
} | ||
|
||
// Error implements the error interface. It uses the default message of the | ||
// wrapped error. This is what will be shown in the services' logs. | ||
func (ae *AuthError) Error() string { | ||
return ae.msg | ||
} | ||
|
||
// IsAuthError checks if an error of type AuthError exists. | ||
func IsAuthError(err error) bool { | ||
var ae *AuthError | ||
return errors.As(err, &ae) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package mid | ||
|
||
import ( | ||
"context" | ||
"net/http" | ||
|
||
"github.com/shawnzxx/service/business/web/auth" | ||
"github.com/shawnzxx/service/foundation/web" | ||
) | ||
|
||
// Authenticate validates a JWT from the `Authorization` header. | ||
func Authenticate(a *auth.Auth) web.Middleware { | ||
m := func(handler web.Handler) web.Handler { | ||
h := func(ctx context.Context, w http.ResponseWriter, r *http.Request) error { | ||
claims, err := a.Authenticate(ctx, r.Header.Get("authorization")) | ||
if err != nil { | ||
return auth.NewAuthError("authenticate: failed: %s", err) | ||
} | ||
|
||
ctx = auth.SetClaims(ctx, claims) | ||
|
||
return handler(ctx, w, r) | ||
} | ||
|
||
return h | ||
} | ||
|
||
return m | ||
} | ||
|
||
// Authorize validates that an authenticated user has at least one role from a | ||
// specified list. This method constructs the actual function that is used. | ||
func Authorize(a *auth.Auth, rule string) web.Middleware { | ||
m := func(handler web.Handler) web.Handler { | ||
h := func(ctx context.Context, w http.ResponseWriter, r *http.Request) error { | ||
claims := auth.GetClaims(ctx) | ||
if claims.Subject == "" { | ||
return auth.NewAuthError("authorize: you are not authorized for that action, no claims") | ||
} | ||
|
||
if err := a.Authorize(ctx, claims, rule); err != nil { | ||
return auth.NewAuthError("authorize: you are not authorized for that action, claims[%v] rule[%v]: %s", claims.Roles, rule, err) | ||
} | ||
|
||
return handler(ctx, w, r) | ||
} | ||
|
||
return h | ||
} | ||
|
||
return m | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.