Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
View Source
var AccessTokenFromRefreshTokenHandler = uhttp.NewHandler( uhttp.WithPostModel(RefreshTokenRequestModel{}, func(r *http.Request, model interface{}, returnCode *int) interface{} { userService := uauth.GetUserService(r) config, err := uauth.ConfigFromRequest(r) if err != nil { return err } req := model.(*RefreshTokenRequestModel) refreshTokenModel, err := uauth.ValidateRefreshToken(req.RefreshToken, userService, config, r.Context()) if err != nil { *returnCode = http.StatusUnauthorized return uauth.MachineError(uauth.ErrInvalidRefreshToken, err) } uiUser, err := userService.GetUiUserByUserName(refreshTokenModel.UserName) if err != nil { *returnCode = http.StatusUnauthorized return uauth.MachineError(uauth.ErrInvalidUser, err) } signedAccessToken, err := uauth.GenerateAccessToken(uiUser, config, r.Context()) if err != nil { return err } return TokenResponseModel{ User: uiUser, AccessToken: signedAccessToken, RefreshToken: req.RefreshToken, } }), )
View Source
var CheckLoginHandler = uhttp.NewHandler( uhttp.WithPostModel(AccessTokenRequestModel{}, func(r *http.Request, model interface{}, returnCode *int) interface{} { config, err := uauth.ConfigFromRequest(r) if err != nil { return err } req := model.(*AccessTokenRequestModel) user, err := uauth.ValidateAccessToken(req.AccessToken, config, r.Context()) if err != nil { return err } return user }), )
CheckLoginHandler for testing a user's webtoken
View Source
var CreateUserHandler = uhttp.NewHandler( uhttp.WithMiddlewares(uauth.AuthJWT(), uauth.CheckPermissions(uauth.CanCreateUsers)), uhttp.WithPostModel( createUserModel{}, func(r *http.Request, model interface{}, returnCode *int) interface{} { userFromRequest := model.(*createUserModel) roleService := uauth.GetRoleService(r) allRoles, err := roleService.List() if err != nil { return err } verifiedRoles := []string{} for _, wantedRole := range userFromRequest.Roles { for _, existingRole := range *allRoles { if wantedRole == existingRole.Name { verifiedRoles = append(verifiedRoles, wantedRole) } } } if len(verifiedRoles) != len(userFromRequest.Roles) { return fmt.Errorf("Not all desired roles for the new user are valid") } hashedPasswordBytes, err := bcrypt.GenerateFromPassword([]byte(userFromRequest.Password), 12) if err != nil { return err } userService := uauth.GetUserService(r) userToBeCreated := uauth.User{ UserName: userFromRequest.UserName, FirstName: userFromRequest.FirstName, LastName: userFromRequest.LastName, Password: uhelpers.PtrToString(string(hashedPasswordBytes)), Roles: &verifiedRoles, } err = userService.CreateUser(&userToBeCreated) if err != nil { return err } return map[string]string{"msg": "created successfully"} }, ), )
View Source
var DeleteRefreshTokenHandler = uhttp.NewHandler( uhttp.WithMiddlewares(uauth.AuthJWT()), uhttp.WithPostModel(deleteRefreshTokenRequest{}, func(r *http.Request, model interface{}, returnCode *int) interface{} { userService := uauth.NewUserService(uauth.UserDB(r), uauth.UserDBName(r)) user, err := uauth.UserFromRequest(r) if err != nil { return err } tokenModel := model.(*deleteRefreshTokenRequest) err = userService.RemoveRefreshToken(user.UserName, tokenModel.RefreshToken, r.Context()) if err != nil { return fmt.Errorf("could not delete refreshToken (%s)", err) } *returnCode = http.StatusNoContent return nil }), )
View Source
var DeleteUserHandler = uhttp.NewHandler( uhttp.WithMiddlewares(uauth.AuthJWT(), uauth.CheckPermissions(uauth.CanDeleteUsers)), uhttp.WithRequiredGet(uhttp.R{"userId": uhttp.STRING}), uhttp.WithDelete(func(r *http.Request, returnCode *int) interface{} { service := uauth.GetUserService(r) ID, err := primitive.ObjectIDFromHex(*uhttp.GetAsString("userId", r)) if err != nil { return err } err = service.Delete(ID) if err != nil { return err } return map[string]string{"msg": "Deleted successfully"} }), )
View Source
var GetRolesHandler = uhttp.NewHandler( uhttp.WithMiddlewares(uauth.AuthJWT(), uauth.CheckPermissions(uauth.CanReadUsers)), uhttp.WithGet(func(r *http.Request, returnCode *int) interface{} { user, err := uauth.UserFromRequest(r) if err != nil { return err } rolesService := uauth.GetRoleService(r) roles, err := rolesService.GetMultipleByName(*user.Roles) if err != nil { return err } return map[string]interface{}{ "success": true, "roles": roles, } }), )
RolesGetHandler for getting days for the logged in user
View Source
var GetUserHandler = uhttp.NewHandler( uhttp.WithMiddlewares(uauth.AuthJWT(), uauth.CheckPermissions(uauth.CanReadUsers)), uhttp.WithRequiredGet(uhttp.R{"userId": uhttp.STRING}), uhttp.WithGet(func(r *http.Request, returnCode *int) interface{} { service := uauth.GetUserService(r) ID, err := primitive.ObjectIDFromHex(*uhttp.GetAsString("userId", r)) if err != nil { return err } userFromDb, err := service.GetUiUserByUserID(ID) if err != nil { return err } userFromDb.Password = nil return *userFromDb }), )
View Source
var ListRefreshTokensHandler = uhttp.NewHandler( uhttp.WithMiddlewares(uauth.AuthJWT()), uhttp.WithGet(func(r *http.Request, returnCode *int) interface{} { user, err := uauth.UserFromRequest(r) if err != nil { return err } userService := uauth.GetUserService(r) tokens, err := userService.ListRefreshTokens(user.UserName, r.Context()) if err != nil { return fmt.Errorf("could not find refreshTokens (%s)", err) } return map[string]interface{}{ "refreshTokens": tokens, } }), )
View Source
var ListRolesHandler = uhttp.NewHandler( uhttp.WithMiddlewares(uauth.AuthJWT(), uauth.CheckPermissions(uauth.CanReadUsers)), uhttp.WithGet(func(r *http.Request, returnCode *int) interface{} { roleService := uauth.GetRoleService(r) rolesFromDb, err := roleService.List() if err != nil { return err } return *rolesFromDb }), )
View Source
var ListUsersHandler = uhttp.NewHandler( uhttp.WithMiddlewares(uauth.AuthJWT(), uauth.CheckPermissions(uauth.CanReadUsers)), uhttp.WithGet(func(r *http.Request, returnCode *int) interface{} { userService := uauth.GetUserService(r) usersFromDb, err := userService.List() if err != nil { return err } users := *usersFromDb for index := range users { user := &users[index] user.Password = nil } return *usersFromDb }), )
View Source
var LoginHandler = uhttp.NewHandler( uhttp.WithPostModel(LoginRequestModel{}, func(r *http.Request, model interface{}, returnCode *int) interface{} { config, err := uauth.ConfigFromRequest(r) if err != nil { return err } loginRequest := model.(*LoginRequestModel) userService := uauth.NewUserService(uauth.UserDB(r), uauth.UserDBName(r)) uiUser, err := userService.GetUIUserByUserNameAndCheckPassword(loginRequest.GetUserName(), loginRequest.GetPassword()) if err != nil { *returnCode = http.StatusUnauthorized return uauth.MachineError(uauth.ErrInvalidUser, fmt.Errorf("No user with this name/password exists (%s)", err)) } signedAccessToken, err := uauth.GenerateAccessToken(uiUser, config, r.Context()) if err != nil { return err } err = userService.DeleteExpiredRefreshTokens(uiUser.UserName, r.Context()) if err != nil { return err } signedRefreshToken, err := uauth.GenerateRefreshToken(uiUser.UserName, userService, r.Header.Get("User-Agent"), config, r.Context()) if err != nil { return err } return TokenResponseModel{ User: uiUser, AccessToken: signedAccessToken, RefreshToken: signedRefreshToken, } }), )
LoginHandler handler for getting JSON web token
View Source
var RenewRefreshTokenHandler = uhttp.NewHandler( uhttp.WithPostModel(RefreshTokenRequestModel{}, func(r *http.Request, model interface{}, returnCode *int) interface{} { config, err := uauth.ConfigFromRequest(r) if err != nil { return err } userService := uauth.NewUserService(uauth.UserDB(r), uauth.UserDBName(r)) req := model.(*RefreshTokenRequestModel) refreshTokenModel, err := uauth.ValidateRefreshToken(req.RefreshToken, userService, config, r.Context()) if err != nil { *returnCode = http.StatusUnauthorized return uauth.MachineError(uauth.ErrInvalidRefreshToken, err) } err = userService.RemoveRefreshToken(refreshTokenModel.UserName, req.RefreshToken, r.Context()) if err != nil { return err } err = userService.DeleteExpiredRefreshTokens(refreshTokenModel.UserName, r.Context()) if err != nil { return err } signedRefreshToken, err := uauth.GenerateRefreshToken(refreshTokenModel.UserName, userService, refreshTokenModel.Device, config, r.Context()) if err != nil { return err } uiUser, err := userService.GetUiUserByUserName(refreshTokenModel.UserName) if err != nil { return err } signedAccessToken, err := uauth.GenerateAccessToken(uiUser, config, r.Context()) if err != nil { return err } return TokenResponseModel{ User: uiUser, AccessToken: signedAccessToken, RefreshToken: signedRefreshToken, } }), )
Trade in an old refresh-token for a new one
View Source
var UpdateUserHandler = uhttp.NewHandler( uhttp.WithMiddlewares(uauth.AuthJWT()), uhttp.WithPostModel(uauth.User{}, func(r *http.Request, model interface{}, returnCode *int) interface{} { user, err := uauth.UserFromRequest(r) if err != nil { return err } userFromRequest := model.(*uauth.User) service := uauth.GetUserService(r) if user.ID == nil { return fmt.Errorf("UserID is not set") } userFromDb, err := service.Get(*userFromRequest.ID) if err != nil { return err } if user.ID != userFromDb.ID && !user.CheckPermission(uauth.CanUpdateUsers) { return fmt.Errorf("User does not have the required permission: %s", uauth.CanUpdateUsers) } if !user.CheckPermission(uauth.CanUpdateUsers) { userFromRequest.Roles = nil } if userFromRequest.Roles != nil { roleService := uauth.NewRoleService(uauth.UserDB(r), uauth.UserDBName(r)) allRoles, err := roleService.List() if err != nil { return err } verifiedRoles := []string{} for _, wantedRole := range *userFromRequest.Roles { for _, existingRole := range *allRoles { if wantedRole == existingRole.Name { verifiedRoles = append(verifiedRoles, wantedRole) } } } if len(verifiedRoles) != len(*userFromRequest.Roles) { return fmt.Errorf("Not all desired roles for the new user are valid") } } if userFromRequest.Password != nil { hashedPassword, err := bcrypt.GenerateFromPassword([]byte(*userFromRequest.Password), 12) if err != nil { return err } userFromRequest.Password = uhelpers.PtrToString(string(hashedPassword)) } userFromRequest.UserName = userFromDb.UserName err = service.Update(*userFromRequest) if err != nil { return err } return map[string]string{"msg": "Updated successfully"} }), )
Functions ¶
func CreateDefaultHandlers ¶
Types ¶
type AccessTokenRequestModel ¶ added in v1.0.65
type AccessTokenRequestModel struct {
AccessToken string `json:"accessToken"`
}
type LoginRequestModel ¶ added in v1.0.66
type LoginRequestModel struct { User *uauth.User `json:"user,omitempty"` UserName *string `json:"userName,omitempty"` Password *string `json:"password,omitempty"` }
func (*LoginRequestModel) GetPassword ¶ added in v1.0.66
func (l *LoginRequestModel) GetPassword() string
func (*LoginRequestModel) GetUserName ¶ added in v1.0.66
func (l *LoginRequestModel) GetUserName() string
type RefreshTokenRequestModel ¶ added in v1.0.65
type RefreshTokenRequestModel struct {
RefreshToken string `json:"refreshToken"`
}
Source Files ¶
- APIModels.go
- AccessTokenFromRefreshTokenHandler.go
- CheckLoginHandler.go
- CreateDefaultHandlers.go
- CreateUserHandler.go
- DeleteRefreshTokenHandler.go
- DeleteUserHandler.go
- GetRolesHandler.go
- GetUserHandler.go
- ListRefreshTokensHandler.go
- ListRolesHandler.go
- ListUsersHandler.go
- LoginHandler.go
- RenewRefreshTokenHandler.go
- UpdateUserHandler.go
Click to show internal directories.
Click to hide internal directories.