| 39 | } |
| 40 | |
| 41 | func ResolveRequest(rw http.ResponseWriter, r *http.Request, opts ResolveRequestOptions) (*SignedToken, bool) { |
| 42 | appReq := opts.AppRequest.Normalize() |
| 43 | err := appReq.Check() |
| 44 | if err != nil { |
| 45 | // This is a 500 since it's a coder server or proxy that's making this |
| 46 | // request struct based on details from the request. The values should |
| 47 | // already be validated before they are put into the struct. |
| 48 | WriteWorkspaceApp500(opts.Logger, opts.DashboardURL, rw, r, &appReq, err, "invalid app request") |
| 49 | return nil, false |
| 50 | } |
| 51 | |
| 52 | token, ok := opts.SignedTokenProvider.FromRequest(r) |
| 53 | if ok && token.MatchesRequest(appReq) { |
| 54 | // The request has a valid signed app token and it matches the request. |
| 55 | return token, true |
| 56 | } |
| 57 | |
| 58 | issueReq := IssueTokenRequest{ |
| 59 | AppRequest: appReq, |
| 60 | PathAppBaseURL: opts.PathAppBaseURL.String(), |
| 61 | AppHostname: opts.AppHostname, |
| 62 | SessionToken: opts.Cookies.TokenFromRequest(r, appReq.AccessMethod), |
| 63 | AppPath: opts.AppPath, |
| 64 | AppQuery: opts.AppQuery, |
| 65 | } |
| 66 | |
| 67 | token, tokenStr, ok := opts.SignedTokenProvider.Issue(r.Context(), rw, r, issueReq) |
| 68 | if !ok { |
| 69 | return nil, false |
| 70 | } |
| 71 | |
| 72 | // Write the signed app token cookie. |
| 73 | // |
| 74 | // For path apps, this applies to only the path app base URL on the current |
| 75 | // domain, e.g. |
| 76 | // /@user/workspace[.agent]/apps/path-app/ |
| 77 | // |
| 78 | // For subdomain apps, this applies to the entire subdomain, e.g. |
| 79 | // app--agent--workspace--user.apps.example.com |
| 80 | http.SetCookie(rw, opts.CookieCfg.Apply(&http.Cookie{ |
| 81 | Name: codersdk.SignedAppTokenCookie, |
| 82 | Value: tokenStr, |
| 83 | Path: appReq.BasePath, |
| 84 | HttpOnly: true, |
| 85 | Expires: token.Expiry.Time(), |
| 86 | })) |
| 87 | |
| 88 | return token, true |
| 89 | } |
| 90 | |
| 91 | // SignedTokenProvider provides signed workspace app tokens (aka. app tickets). |
| 92 | type SignedTokenProvider interface { |