PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/routers/user/auth.go

https://gitlab.com/willjharmer/kanban
Go | 129 lines | 101 code | 22 blank | 6 comment | 16 complexity | a45cacc62026d546eb3cc6b9bbd40b51 MD5 | raw file
  1. package user
  2. import (
  3. "fmt"
  4. "gitlab.com/leanlabsio/kanban/models"
  5. "gitlab.com/leanlabsio/kanban/modules/auth"
  6. "gitlab.com/leanlabsio/kanban/modules/middleware"
  7. "gopkg.in/macaron.v1"
  8. "log"
  9. "net/http"
  10. )
  11. // OauthUrl redirects to url for authorisation
  12. func OauthUrl(ctx *middleware.Context) {
  13. ctx.Redirect(models.AuthCodeURL(ctx.Query("provider")))
  14. }
  15. // OauthLogin logins with gitlab and get access token
  16. func OauthLogin(ctx *middleware.Context, form auth.Oauth2) {
  17. tok, err := models.Exchange(form.Provider, form.Code)
  18. if err != nil {
  19. log.Printf("%s", err.Error())
  20. ctx.JSON(http.StatusBadRequest, models.ResponseError{
  21. Success: false,
  22. Message: err.Error(),
  23. })
  24. return
  25. }
  26. user, err := models.UserOauthSignIn(form.Provider, tok)
  27. if err != nil {
  28. log.Printf("%s", err.Error())
  29. ctx.JSON(http.StatusBadRequest, models.ResponseError{
  30. Success: false,
  31. Message: err.Error(),
  32. })
  33. return
  34. }
  35. user, err = models.LoadByToken(user, ctx.Provider)
  36. if err != nil {
  37. log.Printf("%s", err.Error())
  38. ctx.JSON(http.StatusBadRequest, models.ResponseError{
  39. Success: false,
  40. Message: err.Error(),
  41. })
  42. return
  43. }
  44. user.Username = fmt.Sprintf("%s_%s", user.Username, ctx.Provider)
  45. _, err = models.UpdateUser(user)
  46. // todo add validation by oauth provider
  47. if err != nil {
  48. user, err = models.CreateUser(user)
  49. }
  50. if err != nil {
  51. log.Printf("%s", err.Error())
  52. ctx.JSON(http.StatusInternalServerError, models.ResponseError{
  53. Success: false,
  54. Message: err.Error(),
  55. })
  56. return
  57. }
  58. tokens, err := user.SignedString()
  59. if err != nil {
  60. log.Printf("%s", err.Error())
  61. ctx.JSON(http.StatusBadRequest, models.ResponseError{
  62. Success: false,
  63. Message: err.Error(),
  64. })
  65. return
  66. }
  67. ctx.JSON(http.StatusOK, auth.ResponseAuth{
  68. Success: true,
  69. Token: tokens,
  70. })
  71. }
  72. // SignIn registers with user data
  73. func SignIn(ctx *macaron.Context, form auth.SignIn) {
  74. u, err := models.UserSignIn(form.Uname, form.Pass)
  75. if err != nil {
  76. ctx.JSON(http.StatusBadRequest, models.ResponseError{
  77. Success: false,
  78. Message: err.Error(),
  79. })
  80. return
  81. }
  82. tokens, _ := u.SignedString()
  83. ctx.JSON(http.StatusOK, auth.ResponseAuth{
  84. Success: true,
  85. Token: tokens,
  86. })
  87. }
  88. // SignUp sing ups with data
  89. func SignUp(ctx *middleware.Context, form auth.SignUp) {
  90. u, err := models.UserSignUp(form.Uname, form.Email, form.Pass, form.Token, ctx.Provider)
  91. if err != nil {
  92. ctx.JSON(http.StatusBadRequest, models.ResponseError{
  93. Success: false,
  94. Message: err.Error(),
  95. })
  96. return
  97. }
  98. tokens, _ := u.SignedString()
  99. ctx.JSON(http.StatusOK, auth.ResponseAuth{
  100. Success: true,
  101. Token: tokens,
  102. })
  103. }
  104. // OauthHandler handles request from other services
  105. func OauthHandler(ctx *middleware.Context) {
  106. ctx.HTML(200, "templates/oauth")
  107. }