|
| 1 | +package authentication |
| 2 | + |
| 3 | +import ( |
| 4 | + "crypto/rand" |
| 5 | + "encoding/base64" |
| 6 | + "html/template" |
| 7 | + "main/service" |
| 8 | + "net/http" |
| 9 | +) |
| 10 | + |
| 11 | +type authenticationPageController struct { |
| 12 | + *service.Service |
| 13 | +} |
| 14 | + |
| 15 | +func NewAuthenticationController(s *service.Service) AuthenticationPageController { |
| 16 | + return &authenticationPageController{ |
| 17 | + Service: s, |
| 18 | + } |
| 19 | +} |
| 20 | + |
| 21 | +func (a *authenticationPageController) CallbackHandler(w http.ResponseWriter, r *http.Request) { |
| 22 | + state, err := a.Authenticator.GetStringValue(r, "auth-session", "state") |
| 23 | + if err != nil { |
| 24 | + http.Error(w, err.Error(), http.StatusInternalServerError) |
| 25 | + return |
| 26 | + } |
| 27 | + |
| 28 | + if r.URL.Query().Get("state") != state { |
| 29 | + http.Error(w, "Invalid state parameter", http.StatusBadRequest) |
| 30 | + return |
| 31 | + } |
| 32 | + |
| 33 | + //Retrieve token and save data on session store |
| 34 | + u, err := a.Authenticator.ProcessToken(r.URL.Query().Get("code")) |
| 35 | + if err != nil { |
| 36 | + http.Error(w, err.Error(), http.StatusInternalServerError) |
| 37 | + return |
| 38 | + } |
| 39 | + |
| 40 | + data := map[string]interface{}{ |
| 41 | + "id_token": u.IdToken, |
| 42 | + "access": u.AccessToken, |
| 43 | + "profile": u.Profile, |
| 44 | + "refresh_token": u.RefreshToken, |
| 45 | + "expiry": u.Expiry, |
| 46 | + } |
| 47 | + |
| 48 | + err = a.Authenticator.SaveOnSession(&w, r, "auth-session", data) |
| 49 | + |
| 50 | + if err != nil { |
| 51 | + http.Error(w, err.Error(), http.StatusInternalServerError) |
| 52 | + return |
| 53 | + } |
| 54 | + |
| 55 | + // Redirect to index |
| 56 | + http.Redirect(w, r, "/", http.StatusSeeOther) |
| 57 | +} |
| 58 | + |
| 59 | +func (a *authenticationPageController) LoginHandler(w http.ResponseWriter, r *http.Request) { |
| 60 | + // Generate random state |
| 61 | + b := make([]byte, 32) |
| 62 | + _, err := rand.Read(b) |
| 63 | + |
| 64 | + if err != nil { |
| 65 | + http.Error(w, err.Error(), http.StatusInternalServerError) |
| 66 | + return |
| 67 | + } |
| 68 | + state := base64.StdEncoding.EncodeToString(b) |
| 69 | + |
| 70 | + data := map[string]interface{}{ |
| 71 | + "state": state, |
| 72 | + } |
| 73 | + |
| 74 | + err = a.Authenticator.SaveOnSession(&w, r, "auth-session", data) |
| 75 | + |
| 76 | + if err != nil { |
| 77 | + http.Error(w, err.Error(), http.StatusInternalServerError) |
| 78 | + return |
| 79 | + } |
| 80 | + |
| 81 | + http.Redirect(w, r, a.Authenticator.GetAuthCodeURL(state), http.StatusTemporaryRedirect) |
| 82 | +} |
| 83 | + |
| 84 | +func (a *authenticationPageController) LoginRedirectHandler(w http.ResponseWriter, r *http.Request) { |
| 85 | + q := r.URL.Query() |
| 86 | + redirect := "/" |
| 87 | + if len(q["redirect"]) > 0 { |
| 88 | + redirect = q["redirect"][0] |
| 89 | + } |
| 90 | + data := map[string]interface{}{ |
| 91 | + "redirect": redirect, |
| 92 | + } |
| 93 | + |
| 94 | + c := http.Cookie{ |
| 95 | + Name: "auth-session", |
| 96 | + MaxAge: -1} |
| 97 | + http.SetCookie(w, &c) |
| 98 | + |
| 99 | + tmpl := template.Must(template.ParseFiles("templates/loginredirect.html")) |
| 100 | + tmpl.Execute(w, data) |
| 101 | +} |
| 102 | + |
| 103 | +func (a *authenticationPageController) LogoutHandler(w http.ResponseWriter, r *http.Request) { |
| 104 | + err := a.Authenticator.ClearFromSession(&w, r, "auth-session") |
| 105 | + if err != nil { |
| 106 | + http.Error(w, err.Error(), http.StatusInternalServerError) |
| 107 | + } |
| 108 | + |
| 109 | + url, err := a.Authenticator.GetLogoutURL() |
| 110 | + if err != nil { |
| 111 | + http.Error(w, err.Error(), http.StatusInternalServerError) |
| 112 | + return |
| 113 | + } |
| 114 | + |
| 115 | + http.Redirect(w, r, url, http.StatusTemporaryRedirect) |
| 116 | +} |
0 commit comments