Documentation
¶
Overview ¶
This auth package is an example of how to do authentication and authorization in goradd. It will get you started, but you will likely need to make some changes to fit your particular requirements.
The code below expects you to have a User table in your database. It expects it to have an ID field as a unique identifier for the user, and a PasswordHash field, which should be a character string of at least 256 characters and that will store the hashed password. This system never stores a cleartext password.
You will also need to call PutContext to put the authorization context into the http request context. Call that from app.PutContext().
Index ¶
- Constants
- func Authorize(ctx context.Context, permissionLevel model.UserType) bool
- func CurrentUser(ctx context.Context) *model.User
- func HashPassword(password string) string
- func Logout(ctx context.Context)
- func PutContext(ctx context.Context) context.Context
- func SetCurrentUserID(ctx context.Context, id string)
- func ValidatePassword(password string, userName string) (passwordErr, userNameErr string)
- func VerifyPassword(user *model.User, password string) bool
- type Context
Constants ¶
const AuthContextKey = goradd.ContextKey("app.auth")
Variables ¶
This section is empty.
Functions ¶
func Authorize ¶
Authorize authorizes the current user based on a level of authorization. There are other ways to do this, but this is just an example. For instance, you could authorize based on specific permissions granted, and use a bitfield to OR those together for a more complex authorization system.
func CurrentUser ¶
CurrentUser returns the currently logged in user object, or nil if no user is logged in. It will cache the user information and store it in the context for future reference in the current request, so that the database does not need to be queried each time. It gets the current user out of the sesssion. If there is no current user, it returns nil.
func HashPassword ¶
func PutContext ¶
PutContext allocates our application specific context object and returns it so we can get to it later as the context gets passed around the application. Call this from app.PutContext().
func SetCurrentUserID ¶
SetCurrentUserID will set the current user to a specific id.
func ValidatePassword ¶
ValidatePassword enforces the rules about what makes up a good enough password or userName for our system You can also use a 3rd party library to evaluate password strength.