-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
ssc.go
56 lines (46 loc) · 1.58 KB
/
ssc.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
package httpapi
import (
"fmt"
"net/http"
"github.com/gorilla/mux"
"github.com/sourcegraph/log"
"github.com/sourcegraph/sourcegraph/cmd/frontend/internal/cody"
"github.com/sourcegraph/sourcegraph/cmd/frontend/internal/ssc"
"github.com/sourcegraph/sourcegraph/internal/database"
)
// newSSCRefreshCodyRateLimitHandler returns an http.Handler to trigger cody's rate limit refresh for a user
// TODO(sourcegraph#59625) remove as part of adding SAMSActor source
func newSSCRefreshCodyRateLimitHandler(logger log.Logger, db database.DB) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
samsAccountID := mux.Vars(r)["samsAccountID"]
if samsAccountID == "" {
http.Error(w, "missing uuid", http.StatusBadRequest)
return
}
oidcAccounts, err := db.UserExternalAccounts().List(ctx, database.ExternalAccountsListOptions{
AccountID: samsAccountID,
ServiceType: "openidconnect",
ServiceID: ssc.GetSAMSServiceID(),
LimitOffset: &database.LimitOffset{
Limit: 1,
},
})
if err != nil {
logger.Error("error getting oidc accounts", log.Error(err))
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
if len(oidcAccounts) == 0 {
w.WriteHeader(http.StatusNotFound)
return
}
userID := oidcAccounts[0].UserID
if err, statusCode := cody.RefreshGatewayRateLimits(ctx, userID, db); err != nil {
logger.Error(fmt.Sprintf("error refreshing gateway rate limits: %d", statusCode), log.Error(err))
http.Error(w, err.Error(), statusCode)
return
}
w.WriteHeader(http.StatusOK)
})
}