http.go154 lines, 4.2 KB
| 1 | package auth |
| 2 | |
| 3 | import ( |
| 4 | "net/http" |
| 5 | "time" |
| 6 | |
| 7 | "github.com/fiston-user/payments/internal/platform/authn" |
| 8 | "github.com/fiston-user/payments/internal/platform/httpx" |
| 9 | "github.com/google/uuid" |
| 10 | ) |
| 11 | |
| 12 | // The API's JSON shapes. |
| 13 | type ( |
| 14 | credentials struct { |
| 15 | Email string `json:"email"` |
| 16 | Password string `json:"password"` |
| 17 | } |
| 18 | refreshRequest struct { |
| 19 | RefreshToken string `json:"refresh_token"` |
| 20 | } |
| 21 | userResponse struct { |
| 22 | ID uuid.UUID `json:"id"` |
| 23 | Email string `json:"email"` |
| 24 | CreatedAt time.Time `json:"created_at"` |
| 25 | } |
| 26 | // tokenResponse has the shape of an OAuth 2.0 token response, which |
| 27 | // client libraries already know how to read. |
| 28 | tokenResponse struct { |
| 29 | AccessToken string `json:"access_token"` |
| 30 | TokenType string `json:"token_type"` |
| 31 | ExpiresIn int `json:"expires_in"` // seconds |
| 32 | RefreshToken string `json:"refresh_token"` |
| 33 | } |
| 34 | ) |
| 35 | |
| 36 | func toUserResponse(u User) userResponse { |
| 37 | return userResponse{ID: u.ID, Email: u.Email, CreatedAt: u.CreatedAt} |
| 38 | } |
| 39 | |
| 40 | // Handler serves the auth API over HTTP. |
| 41 | type Handler struct { |
| 42 | svc *Service |
| 43 | verifier *authn.Verifier |
| 44 | } |
| 45 | |
| 46 | // NewHandler returns a Handler that serves svc. verifier checks the access |
| 47 | // tokens of requests to protected routes. |
| 48 | func NewHandler(svc *Service, verifier *authn.Verifier) *Handler { |
| 49 | return &Handler{svc: svc, verifier: verifier} |
| 50 | } |
| 51 | |
| 52 | // Register adds the auth API's routes to mux. |
| 53 | func (h *Handler) Register(mux *http.ServeMux) { |
| 54 | mux.Handle("POST /v1/auth/register", httpx.HandlerFunc(h.registerUser)) |
| 55 | mux.Handle("POST /v1/auth/login", httpx.HandlerFunc(h.login)) |
| 56 | mux.Handle("POST /v1/auth/refresh", httpx.HandlerFunc(h.refresh)) |
| 57 | mux.Handle("POST /v1/auth/logout", httpx.HandlerFunc(h.logout)) |
| 58 | mux.Handle("GET /v1/auth/me", h.verifier.Require(httpx.HandlerFunc(h.me))) |
| 59 | } |
| 60 | |
| 61 | // RegisterInternal adds the routes for our own services to mux. The gateway |
| 62 | // never routes /internal/ to us. |
| 63 | func (h *Handler) RegisterInternal(mux *http.ServeMux) { |
| 64 | mux.Handle("GET /internal/users/{id}", httpx.HandlerFunc(h.user)) |
| 65 | } |
| 66 | |
| 67 | func (h *Handler) registerUser(w http.ResponseWriter, r *http.Request) error { |
| 68 | var req credentials |
| 69 | if err := httpx.DecodeJSON(w, r, &req); err != nil { |
| 70 | return err |
| 71 | } |
| 72 | u, err := h.svc.Register(r.Context(), req.Email, req.Password) |
| 73 | if err != nil { |
| 74 | return err |
| 75 | } |
| 76 | httpx.WriteJSON(w, r, http.StatusCreated, toUserResponse(u)) |
| 77 | return nil |
| 78 | } |
| 79 | |
| 80 | func (h *Handler) login(w http.ResponseWriter, r *http.Request) error { |
| 81 | var req credentials |
| 82 | if err := httpx.DecodeJSON(w, r, &req); err != nil { |
| 83 | return err |
| 84 | } |
| 85 | t, err := h.svc.Login(r.Context(), req.Email, req.Password) |
| 86 | if err != nil { |
| 87 | return err |
| 88 | } |
| 89 | writeTokens(w, r, t) |
| 90 | return nil |
| 91 | } |
| 92 | |
| 93 | func (h *Handler) refresh(w http.ResponseWriter, r *http.Request) error { |
| 94 | var req refreshRequest |
| 95 | if err := httpx.DecodeJSON(w, r, &req); err != nil { |
| 96 | return err |
| 97 | } |
| 98 | t, err := h.svc.Refresh(r.Context(), req.RefreshToken) |
| 99 | if err != nil { |
| 100 | return err |
| 101 | } |
| 102 | writeTokens(w, r, t) |
| 103 | return nil |
| 104 | } |
| 105 | |
| 106 | func (h *Handler) logout(w http.ResponseWriter, r *http.Request) error { |
| 107 | var req refreshRequest |
| 108 | if err := httpx.DecodeJSON(w, r, &req); err != nil { |
| 109 | return err |
| 110 | } |
| 111 | if err := h.svc.Logout(r.Context(), req.RefreshToken); err != nil { |
| 112 | return err |
| 113 | } |
| 114 | w.WriteHeader(http.StatusNoContent) |
| 115 | return nil |
| 116 | } |
| 117 | |
| 118 | // me returns the user the access token belongs to. Require guarantees there |
| 119 | // is one. |
| 120 | func (h *Handler) me(w http.ResponseWriter, r *http.Request) error { |
| 121 | id, _ := authn.UserID(r.Context()) |
| 122 | u, err := h.svc.User(r.Context(), id) |
| 123 | if err != nil { |
| 124 | return err |
| 125 | } |
| 126 | httpx.WriteJSON(w, r, http.StatusOK, toUserResponse(u)) |
| 127 | return nil |
| 128 | } |
| 129 | |
| 130 | // user returns any user, by ID, for services like notification that only |
| 131 | // know IDs. |
| 132 | func (h *Handler) user(w http.ResponseWriter, r *http.Request) error { |
| 133 | id, err := uuid.Parse(r.PathValue("id")) |
| 134 | if err != nil { |
| 135 | return ErrUserNotFound |
| 136 | } |
| 137 | u, err := h.svc.User(r.Context(), id) |
| 138 | if err != nil { |
| 139 | return err |
| 140 | } |
| 141 | httpx.WriteJSON(w, r, http.StatusOK, toUserResponse(u)) |
| 142 | return nil |
| 143 | } |
| 144 | |
| 145 | // writeTokens sends a pair of tokens. No browser or proxy may cache them. |
| 146 | func writeTokens(w http.ResponseWriter, r *http.Request, t Tokens) { |
| 147 | w.Header().Set("Cache-Control", "no-store") |
| 148 | httpx.WriteJSON(w, r, http.StatusOK, tokenResponse{ |
| 149 | AccessToken: t.Access, |
| 150 | TokenType: "Bearer", |
| 151 | ExpiresIn: int(t.ExpiresIn.Seconds()), |
| 152 | RefreshToken: t.Refresh, |
| 153 | }) |
| 154 | } |