|
2 | 2 | "Defines routes for mocking URS" |
3 | 3 | (:require |
4 | 4 | [cheshire.core :as json] |
| 5 | + [clj-time.core :as t] |
5 | 6 | [clojure.data.codec.base64 :as b64] |
6 | 7 | [clojure.string :as string] |
7 | 8 | [cmr.common.mime-types :as mt] |
8 | 9 | [cmr.common.services.errors :as errors] |
| 10 | + [cmr.common.time-keeper :as time-keeper] |
| 11 | + [cmr.common.util :as common-util] |
9 | 12 | [cmr.common.xml :as cx] |
10 | 13 | [cmr.mock-echo.data.urs-db :as urs-db] |
11 | 14 | [cmr.transmit.config :as transmit-config] |
12 | 15 | [compojure.core :refer :all])) |
13 | 16 |
|
| 17 | +(def launchpad-token-validations |
| 18 | + "Tracks when launchpad tokens were first validated to simulate absolute token expiration. |
| 19 | + Map of {token -> {:first-validated-at timestamp :expires-in seconds}}" |
| 20 | + (atom {})) |
| 21 | + |
| 22 | +(defn reset-launchpad-tokens! |
| 23 | + "Resets the launchpad token validation tracking. Called by test fixtures." |
| 24 | + [] |
| 25 | + (reset! launchpad-token-validations {})) |
| 26 | + |
14 | 27 | (defn get-user |
15 | 28 | "Processes a request to get a user." |
16 | 29 | [context name] |
|
44 | 57 | {:status 404 :body "Not found.\n"})) |
45 | 58 |
|
46 | 59 | (defn get-launchpad-user |
47 | | - "Processes a request to get a user using their launchpad token." |
| 60 | + "Processes a request to get a user using their launchpad token. |
| 61 | + Supports specific test tokens that return different status codes for testing error handling. |
| 62 | + Simulates real EDL behavior where tokens have absolute expiration times." |
48 | 63 | [token] |
49 | 64 | (case token |
50 | 65 | "ABC-1ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ" |
51 | | - {:status 200 :body {:uid "user1" :lp_token_expires_in 1600}} |
| 66 | + (let [expires-in 1600 |
| 67 | + validation-info (get @launchpad-token-validations token)] |
| 68 | + (if validation-info |
| 69 | + (let [elapsed-seconds (t/in-seconds (t/interval (:first-validated-at validation-info) (time-keeper/now)))] |
| 70 | + (if (>= elapsed-seconds expires-in) |
| 71 | + {:status 401 :body {:error (format "Launchpad token (partially redacted) [%s] has expired." |
| 72 | + (common-util/scrub-token token))}} |
| 73 | + {:status 200 :body {:uid "user1" :lp_token_expires_in (- expires-in elapsed-seconds)}})) |
| 74 | + (do |
| 75 | + (swap! launchpad-token-validations assoc token {:first-validated-at (time-keeper/now) :expires-in expires-in}) |
| 76 | + {:status 200 :body {:uid "user1" :lp_token_expires_in expires-in}}))) |
| 77 | + |
| 78 | + "ABC-429-ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ" |
| 79 | + {:status 429 :body {:error "Rate limit exceeded"}} |
| 80 | + |
| 81 | + "ABC-504-ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ" |
| 82 | + {:status 504 :body {:error "Gateway timeout"}} |
| 83 | + |
| 84 | + "ABC-INV-ZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZZ" |
| 85 | + {:status 401 :body {:error "Invalid token"}} |
| 86 | + |
52 | 87 | {:status 400 :body {:error "Launchpad SSO authentication failed"}})) |
53 | 88 |
|
54 | 89 | (defn get-user-info |
|
0 commit comments