MCPcopy Create free account
hub / github.com/PerpetualSoftware/pad / TokenAuth

Method TokenAuth

internal/server/middleware_auth.go:131–288  ·  view source on GitHub ↗

TokenAuth middleware checks for an Authorization: Bearer pad_xxx header. If a valid token is found, the associated workspace ID is stored in the request context. If no token header is present the request passes through unchanged (existing localhost behaviour). Invalid or expired tokens receive a 401

(next http.Handler)

Source from the content-addressed store, hash-verified

129// including the very endpoints needed to log in again. See BUG-1227 and
130// the matching IP-change-revoked branch below for the same pattern.
131func (s *Server) TokenAuth(next http.Handler) http.Handler {
132 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
133 auth := r.Header.Get("Authorization")
134 if auth == "" {
135 // No token provided — allow through (localhost access).
136 next.ServeHTTP(w, r)
137 return
138 }
139
140 // Expect "Bearer pad_<64 hex chars>" or "Bearer padsess_<64 hex chars>"
141 if !strings.HasPrefix(auth, "Bearer ") {
142 rejectInvalidBearer(w, r, next, "unauthorized", "Invalid authorization header format")
143 return
144 }
145
146 token := strings.TrimPrefix(auth, "Bearer ")
147 token = strings.TrimSpace(token)
148
149 // Session token (from CLI login)
150 if strings.HasPrefix(token, "padsess_") {
151 session, err := s.store.ValidateSessionContext(r.Context(), token)
152 if err != nil {
153 if r.Context().Err() != nil {
154 return // client gone: nobody to answer, nothing to log (TASK-4278)
155 }
156 writeStoreErrorR(w, r, err, http.StatusInternalServerError, "internal_error", "Session validation failed")
157 return
158 }
159 if session == nil {
160 rejectInvalidBearer(w, r, next, "unauthorized", "Invalid or expired session")
161 return
162 }
163 // Session binding: detect a User-Agent change. Logged in all
164 // modes; the session is revoked + the request rejected only when
165 // PAD_IP_CHANGE_ENFORCE=strict. The UA is client-supplied and
166 // trivially replayed by anyone who already stole the token, so it
167 // stays log-only by default (breaking browser/WebView updates,
168 // DevTools device emulation, and mobile-app rebuilds would hurt
169 // more than it helps). Checked BEFORE the IP change so a stolen
170 // token replayed from a fresh client is killed on the more stable
171 // of the two signals first.
172 switch s.handleSessionUAChange(w, r, session, token) {
173 case sessionIPChangeTerminated:
174 return
175 case sessionIPChangeRevoked:
176 if isPublicAPIPath(r.URL.Path) {
177 next.ServeHTTP(w, r)
178 return
179 }
180 writeError(w, http.StatusUnauthorized, "session_ua_changed",
181 "Session client changed — please log in again.")
182 return
183 }
184 // Session binding: detect IP changes. Log to audit log in all modes;
185 // reject only when PAD_IP_CHANGE_ENFORCE=strict.
186 switch s.handleSessionIPChange(w, r, session, token) {
187 case sessionIPChangeTerminated:
188 return

Callers

nothing calls this directly

Calls 15

handleSessionUAChangeMethod · 0.95
handleSessionIPChangeMethod · 0.95
withTokenPinMethod · 0.95
rejectInvalidBearerFunction · 0.85
writeStoreErrorRFunction · 0.85
isPublicAPIPathFunction · 0.85
writeErrorFunction · 0.85
tokenScopeAllowsFunction · 0.85
writeForbiddenFunction · 0.85
setTokenExpiryWarningFunction · 0.85
WithTokenScopesFunction · 0.85

Tested by

no test coverage detected