NOTE: This will only work in handlers that use the toHandleFunc wrapper.
(ctx context.Context)
| 250 | |
| 251 | // NOTE: This will only work in handlers that use the toHandleFunc wrapper. |
| 252 | func (s *server) getUserID(ctx context.Context) (UserID, error) { |
| 253 | authHdr, ok := ctx.Value(authContextKey{}).(string) |
| 254 | if !ok { |
| 255 | return "", errors.New("no auth in context, is middleware active on this handler?") |
| 256 | } |
| 257 | if authHdr == "" { |
| 258 | return "", errors.New("no 'Authorization' header in request") |
| 259 | } |
| 260 | if !strings.HasPrefix(strings.ToLower(authHdr), "bearer ") { |
| 261 | return "", errors.New("malformed 'Authorization' header had no 'Bearer ' prefix") |
| 262 | } |
| 263 | |
| 264 | // NOTE: We use ParseUnverified here because it's easier and on a self-hosted, |
| 265 | // single person system, we don't really care if it's signed correctly or not. |
| 266 | // That said, one could totally get the public keys from AWS Cognito to actually |
| 267 | // verify the JWT. |
| 268 | claims := jwt.MapClaims{} |
| 269 | _, _, err := s.jwt.ParseUnverified(authHdr[7:], claims) |
| 270 | if err != nil { |
| 271 | return "", fmt.Errorf("failed to parse JWT: %w", err) |
| 272 | } |
| 273 | |
| 274 | sub, err := claims.GetSubject() |
| 275 | if err != nil { |
| 276 | return "", fmt.Errorf("failed to get 'sub' claim from JWT: %w", err) |
| 277 | } |
| 278 | |
| 279 | return UserID(sub), nil |
| 280 | } |
| 281 | |
| 282 | func proxyWS(w http.ResponseWriter, r *http.Request) { |
| 283 | ctx := r.Context() |
no test coverage detected