login.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. package api
  2. import (
  3. "errors"
  4. "fmt"
  5. "net/http"
  6. "net/url"
  7. "time"
  8. jwt "github.com/dgrijalva/jwt-go"
  9. "gogs.carducci-dante.gov.it/karmen/core/config"
  10. "gogs.carducci-dante.gov.it/karmen/core/renderer"
  11. )
  12. func logoutHandler() http.Handler {
  13. fn := func(w http.ResponseWriter, r *http.Request) {
  14. session, err := store.Get(r, "login-session")
  15. if err != nil {
  16. http.Error(w, err.Error(), http.StatusInternalServerError)
  17. return
  18. }
  19. session.Values["token"] = []uint8{}
  20. session.Save(r, w)
  21. http.Redirect(w, r, "/", http.StatusSeeOther)
  22. }
  23. return http.HandlerFunc(fn)
  24. }
  25. func loginHandler() http.Handler {
  26. fn := func(w http.ResponseWriter, r *http.Request) {
  27. if r.Method == "GET" {
  28. renderer.Render["html"](w, r, nil, url.Values{"tpl_layout": []string{"login"}, "tpl_content": []string{"login"}})
  29. }
  30. if r.Method == "POST" {
  31. r.ParseForm()
  32. token, err := getToken(r.FormValue("username"), r.FormValue("password"))
  33. if err != nil {
  34. panic(err)
  35. } else {
  36. session, err := store.Get(r, "login-session")
  37. if err != nil {
  38. panic(err)
  39. }
  40. session.Values["token"] = token
  41. session.Save(r, w)
  42. r.Method = "GET"
  43. http.Redirect(w, r, "/teachers?format=html&tpl_layout=base&tpl_content=teachers", http.StatusSeeOther)
  44. }
  45. }
  46. }
  47. return http.HandlerFunc(fn)
  48. }
  49. func checkCredential(username string, password string) (*User, error) {
  50. if username == config.Config.Admin.Username && password == config.Config.Admin.Password {
  51. return &User{username, true}, nil
  52. }
  53. return nil, errors.New("Authentication failed!")
  54. }
  55. func getToken(username string, password string) ([]byte, error) {
  56. user, err := checkCredential(username, password)
  57. if err != nil {
  58. return nil, err
  59. }
  60. /* Set token claims */
  61. claims := make(map[string]interface{})
  62. claims["admin"] = user.Admin
  63. claims["name"] = user.Name
  64. claims["exp"] = time.Now().Add(time.Hour * 24).Unix()
  65. /* Create the token */
  66. token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims(claims))
  67. /* Sign the token with our secret */
  68. tokenString, err := token.SignedString(signingKey)
  69. if err != nil {
  70. return nil, err
  71. }
  72. return []byte(tokenString), nil
  73. }
  74. func tokenHandler() http.Handler {
  75. fn := func(w http.ResponseWriter, r *http.Request) {
  76. username, password, _ := r.BasicAuth()
  77. user, err := checkCredential(username, password)
  78. if err != nil {
  79. panic(err)
  80. }
  81. /* Set token claims */
  82. claims := make(map[string]interface{})
  83. claims["admin"] = true
  84. claims["name"] = user.Name
  85. claims["exp"] = time.Now().Add(time.Hour * 24).Unix()
  86. /* Create the token */
  87. token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims(claims))
  88. /* Sign the token with our secret */
  89. tokenString, err := token.SignedString(signingKey)
  90. if err != nil {
  91. panic(err)
  92. }
  93. w.Header().Set("Content-Type", "application/json; charset=utf-8")
  94. w.Write([]byte(fmt.Sprintf("{\"Token\":\"%s\",\"User\":\"%s\"}", tokenString, user.Name)))
  95. }
  96. return http.HandlerFunc(fn)
  97. }