Skip to content

Commit

Permalink
add logout route that revokes the token
Browse files Browse the repository at this point in the history
  • Loading branch information
Linesmerrill committed May 30, 2024
1 parent 064deae commit 0a812a9
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 28 deletions.
1 change: 1 addition & 0 deletions api/handlers/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ func (a *App) New() *mux.Router {
apiCreate := r.PathPrefix("/api/v1").Subrouter()

apiCreate.Handle("/auth/token", api.Middleware(http.HandlerFunc(api.CreateToken))).Methods("POST")
apiCreate.Handle("/auth/logout", api.Middleware(http.HandlerFunc(api.RevokeToken))).Methods("DELETE")

apiCreate.Handle("/community/{community_id}", api.Middleware(http.HandlerFunc(c.CommunityHandler))).Methods("GET")
apiCreate.Handle("/community/{community_id}/{owner_id}", api.Middleware(http.HandlerFunc(c.CommunityByCommunityAndOwnerIDHandler))).Methods("GET")
Expand Down
27 changes: 0 additions & 27 deletions api/handlers/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,30 +111,3 @@ func (u User) UserLoginHandler(w http.ResponseWriter, r *http.Request) {
http.Error(w, "Unauthorized", http.StatusUnauthorized)

}

// UserLogoutHandler returns a status code of the user logging out
func (u User) UserLogoutHandler(w http.ResponseWriter, r *http.Request) {
commID := mux.Vars(r)["user_id"]

zap.S().Debugf("user_id: %v", commID)

cID, err := primitive.ObjectIDFromHex(commID)
if err != nil {
config.ErrorStatus("failed to get objectID from Hex", http.StatusBadRequest, w, err)
return
}

dbResp, err := u.DB.FindOne(context.Background(), bson.M{"_id": cID})
if err != nil {
config.ErrorStatus("failed to get user by ID", http.StatusNotFound, w, err)
return
}

b, err := json.Marshal(dbResp)
if err != nil {
config.ErrorStatus("failed to marshal response", http.StatusInternalServerError, w, err)
return
}
w.WriteHeader(http.StatusOK)
w.Write(b)
}
17 changes: 16 additions & 1 deletion api/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@ import (
"crypto/subtle"
"fmt"
"net/http"
"strings"

"time"

"github.com/google/uuid"
"github.com/linesmerrill/police-cad-api/databases"
"github.com/shaj13/go-guardian/auth"
"github.com/shaj13/go-guardian/auth/strategies/basic"
"github.com/shaj13/go-guardian/auth/strategies/bearer"

"github.com/shaj13/go-guardian/auth/strategies/basic"
"github.com/shaj13/go-guardian/store"
"go.mongodb.org/mongo-driver/bson"
"go.uber.org/zap"
Expand Down Expand Up @@ -96,3 +99,15 @@ func (m MiddlewareDB) ValidateUser(ctx context.Context, r *http.Request, email,
}
return nil, fmt.Errorf("invalid credentials")
}

// RevokeToken revokes a token
func RevokeToken(w http.ResponseWriter, r *http.Request) {
reqToken := r.Header.Get("Authorization")
splitToken := strings.Split(reqToken, "Bearer ")
reqToken = splitToken[1]

tokenStrategy := authenticator.Strategy(bearer.CachedStrategyKey)
auth.Revoke(tokenStrategy, reqToken, r)
body := fmt.Sprintf("revoked token: %s \n", reqToken)
w.Write([]byte(body))
}

0 comments on commit 0a812a9

Please sign in to comment.