ListApps returns an http.HandlerFunc that handles GET /oauth2-provider/apps
(db database.Store, accessURL *url.URL)
| 21 | |
| 22 | // ListApps returns an http.HandlerFunc that handles GET /oauth2-provider/apps |
| 23 | func ListApps(db database.Store, accessURL *url.URL) http.HandlerFunc { |
| 24 | return func(rw http.ResponseWriter, r *http.Request) { |
| 25 | ctx := r.Context() |
| 26 | |
| 27 | rawUserID := r.URL.Query().Get("user_id") |
| 28 | if rawUserID == "" { |
| 29 | dbApps, err := db.GetOAuth2ProviderApps(ctx) |
| 30 | if err != nil { |
| 31 | httpapi.InternalServerError(rw, err) |
| 32 | return |
| 33 | } |
| 34 | httpapi.Write(ctx, rw, http.StatusOK, db2sdk.OAuth2ProviderApps(accessURL, dbApps)) |
| 35 | return |
| 36 | } |
| 37 | |
| 38 | userID, err := uuid.Parse(rawUserID) |
| 39 | if err != nil { |
| 40 | httpapi.Write(ctx, rw, http.StatusBadRequest, codersdk.Response{ |
| 41 | Message: "Invalid user UUID", |
| 42 | Detail: fmt.Sprintf("queried user_id=%q", userID), |
| 43 | }) |
| 44 | return |
| 45 | } |
| 46 | |
| 47 | userApps, err := db.GetOAuth2ProviderAppsByUserID(ctx, userID) |
| 48 | if err != nil { |
| 49 | httpapi.InternalServerError(rw, err) |
| 50 | return |
| 51 | } |
| 52 | |
| 53 | sdkApps := make([]codersdk.OAuth2ProviderApp, 0, len(userApps)) |
| 54 | for _, app := range userApps { |
| 55 | sdkApps = append(sdkApps, db2sdk.OAuth2ProviderApp(accessURL, app.OAuth2ProviderApp)) |
| 56 | } |
| 57 | httpapi.Write(ctx, rw, http.StatusOK, sdkApps) |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | // GetApp returns an http.HandlerFunc that handles GET /oauth2-provider/apps/{app} |
| 62 | func GetApp(accessURL *url.URL) http.HandlerFunc { |
no test coverage detected