multitenantStack/controllers/auth.go

128 lines
3.3 KiB
Go
Raw Normal View History

2018-11-07 10:10:51 +00:00
package controllers
import (
auth "multitenantStack/services"
2018-11-07 10:10:51 +00:00
"time"
jwt "github.com/dgrijalva/jwt-go"
)
// AuthController operations for Auth
type AuthController struct {
BaseController
}
// URLMapping ...
func (c *AuthController) URLMapping() {
// This block is used to drastically speed up the annotation -> lookup process
c.Mapping("Login", c.Login)
c.Mapping("GetOne", c.GetOne)
c.Mapping("GetAll", c.GetAll)
c.Mapping("Put", c.Put)
c.Mapping("Delete", c.Delete)
}
// Login Get a JWT token for the user
// @Title Create
// @Description create Auth
// @Param body body models.Auth true "body for Auth content"
// @Success 201 {object} models.Auth
// @Failure 403 body is empty
// @router /login [post]
func (c *AuthController) Login() {
type AuthResponse struct {
Status int
Jwt string
}
if c.Ctx.Input.Method() != "POST" {
c.ServeJSONError("Method not allowed")
2018-11-07 10:10:51 +00:00
return
}
//TODO: did the user send us a token? then just validate and tell him he is logged in
email := c.GetString("email")
password := c.GetString("password")
//TODO: check against main database, get company id and verify password
companyName := "company_1"
companyUserID := 5
//TODO: if found query the company database to get roleID, and name
2018-11-07 10:10:51 +00:00
name := "Lukas"
roleID := 5
2018-11-07 10:10:51 +00:00
tokenString := ""
if email == "admin@admin.at" && password == "my password" {
// The jwtClaims are our trusted clientside session
tokenString = auth.CreateToken(jwt.MapClaims{
"email": email,
"companyName": companyName,
"companyUserID": companyUserID,
2018-11-07 10:10:51 +00:00
"name": name,
"roleID": roleID,
"exp": time.Now().Unix() + 3600,
2018-11-07 10:10:51 +00:00
})
} else {
c.ServeJSONError("Invalid user/password")
2018-11-07 10:10:51 +00:00
return
}
json := AuthResponse{200, tokenString}
c.Data["json"] = &json
c.ServeJSON()
}
// GetOne ...
// @Title GetOne
// @Description get Auth by id
// @Param id path string true "The key for staticblock"
// @Success 200 {object} models.Auth
// @Failure 403 :id is empty
// @router /:id [get]
func (c *AuthController) GetOne() {
}
// GetAll ...
// @Title GetAll
// @Description get Auth
// @Param query query string false "Filter. e.g. col1:v1,col2:v2 ..."
// @Param fields query string false "Fields returned. e.g. col1,col2 ..."
// @Param sortby query string false "Sorted-by fields. e.g. col1,col2 ..."
// @Param order query string false "Order corresponding to each sortby field, if single value, apply to all sortby fields. e.g. desc,asc ..."
// @Param limit query string false "Limit the size of result set. Must be an integer"
// @Param offset query string false "Start position of result set. Must be an integer"
// @Success 200 {object} models.Auth
// @Failure 403
// @router / [get]
func (c *AuthController) GetAll() {
}
// Put ...
// @Title Put
// @Description update the Auth
// @Param id path string true "The id you want to update"
// @Param body body models.Auth true "body for Auth content"
// @Success 200 {object} models.Auth
// @Failure 403 :id is not int
// @router /:id [put]
func (c *AuthController) Put() {
}
// Delete ...
// @Title Delete
// @Description delete the Auth
// @Param id path string true "The id you want to delete"
// @Success 200 {string} delete success!
// @Failure 403 id is empty
// @router /:id [delete]
func (c *AuthController) Delete() {
}