|
| 1 | +package auth |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "log/slog" |
| 7 | + "net/http" |
| 8 | + "net/url" |
| 9 | + "strings" |
| 10 | + |
| 11 | + hydra "github.com/ory/hydra-client-go/v2" |
| 12 | +) |
| 13 | + |
| 14 | +type HydraClientManager struct { |
| 15 | + hydraAdmin *hydra.APIClient |
| 16 | + logger *slog.Logger |
| 17 | +} |
| 18 | + |
| 19 | +func NewHydraClientManager(adminURL string, logger *slog.Logger) *HydraClientManager { |
| 20 | + hydraConfig := hydra.NewConfiguration() |
| 21 | + hydraConfig.Servers = []hydra.ServerConfiguration{ |
| 22 | + { |
| 23 | + URL: adminURL, |
| 24 | + }, |
| 25 | + } |
| 26 | + |
| 27 | + return &HydraClientManager{ |
| 28 | + hydraAdmin: hydra.NewAPIClient(hydraConfig), |
| 29 | + logger: logger, |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +func (h *HydraClientManager) CreateClient(ctx context.Context, clientInfo ClientInfo) (ClientInfo, error) { |
| 34 | + newClient := hydra.OAuth2Client{ |
| 35 | + ClientId: &clientInfo.ClientID, |
| 36 | + Scope: &clientInfo.Scope, |
| 37 | + GrantTypes: []string{"client_credentials"}, |
| 38 | + } |
| 39 | + if clientInfo.ClientSecret != "" { |
| 40 | + newClient.ClientSecret = &clientInfo.ClientSecret |
| 41 | + } |
| 42 | + |
| 43 | + createdClient, response, err := h.hydraAdmin.OAuth2API.CreateOAuth2Client(ctx).OAuth2Client(newClient).Execute() |
| 44 | + if response.StatusCode == 409 { |
| 45 | + return ClientInfo{}, fmt.Errorf("failed to create client: client with id %s already exists", *newClient.ClientId) |
| 46 | + } |
| 47 | + if response.StatusCode == 400 { |
| 48 | + return ClientInfo{}, fmt.Errorf("failed to create client: invalid request") |
| 49 | + } |
| 50 | + if response.StatusCode != 201 { |
| 51 | + return ClientInfo{}, fmt.Errorf("failed to create client: status=%s", response.Status) |
| 52 | + } |
| 53 | + // these can be confusing and related to internal client failures, so handled after http status codes |
| 54 | + if err != nil { |
| 55 | + return ClientInfo{}, fmt.Errorf("failed to create client: %w", err) |
| 56 | + } |
| 57 | + |
| 58 | + return clientInfoFromHydraClient(createdClient), nil |
| 59 | +} |
| 60 | + |
| 61 | +func (h *HydraClientManager) DeleteClientByID(ctx context.Context, clientID string) error { |
| 62 | + response, err := h.hydraAdmin.OAuth2API.DeleteOAuth2Client(ctx, clientID).Execute() |
| 63 | + if response.StatusCode == 404 { |
| 64 | + return fmt.Errorf("client %s not found", clientID) |
| 65 | + } |
| 66 | + if response.StatusCode != 204 { |
| 67 | + return fmt.Errorf("failed to delete client from hydra: status=%s", response.Status) |
| 68 | + } |
| 69 | + // these can be confusing and related to internal client failures, so handled after http status codes |
| 70 | + if err != nil { |
| 71 | + return fmt.Errorf("failed to delete client from hydra: %w", err) |
| 72 | + } |
| 73 | + |
| 74 | + return nil |
| 75 | +} |
| 76 | + |
| 77 | +func (h *HydraClientManager) RetrieveClientByID(ctx context.Context, clientID string) (ClientInfo, error) { |
| 78 | + client, response, err := h.hydraAdmin.OAuth2API.GetOAuth2Client(ctx, clientID).Execute() |
| 79 | + if response.StatusCode == 404 { |
| 80 | + return ClientInfo{}, fmt.Errorf("client %s not found", clientID) |
| 81 | + } |
| 82 | + if response.StatusCode != 200 { |
| 83 | + return ClientInfo{}, fmt.Errorf("failed to retrieve client: status=%s", response.Status) |
| 84 | + } |
| 85 | + // these tend to be confusing and related to internal client failures, so handled after http status codes |
| 86 | + if err != nil { |
| 87 | + return ClientInfo{}, fmt.Errorf("failed to retrieve client: %w", err) |
| 88 | + } |
| 89 | + |
| 90 | + return clientInfoFromHydraClient(client), nil |
| 91 | +} |
| 92 | + |
| 93 | +func (h *HydraClientManager) RetrieveClients(ctx context.Context, q RetrieveClientsRequest) (RetrieveClientsResponse, error) { |
| 94 | + var out RetrieveClientsResponse |
| 95 | + |
| 96 | + clients, response, err := h.hydraAdmin.OAuth2API.ListOAuth2Clients(ctx).PageToken(q.PageToken).Execute() |
| 97 | + if response.StatusCode != 200 { |
| 98 | + return out, fmt.Errorf("failed to retrieve clients: status=%s", response.Status) |
| 99 | + } |
| 100 | + // these can be confusing and related to internal client failures, so handled after http status codes |
| 101 | + if err != nil { |
| 102 | + return out, fmt.Errorf("failed to retrieve clients: %w", err) |
| 103 | + } |
| 104 | + |
| 105 | + out.Clients = make([]ClientInfo, 0, len(clients)) |
| 106 | + for _, client := range clients { |
| 107 | + out.Clients = append(out.Clients, clientInfoFromHydraClient(&client)) |
| 108 | + } |
| 109 | + |
| 110 | + out.NextPageToken = getHydraNextPageToken(response, h.logger) |
| 111 | + return out, nil |
| 112 | +} |
| 113 | + |
| 114 | +func clientInfoFromHydraClient(client *hydra.OAuth2Client) ClientInfo { |
| 115 | + var clientInfo ClientInfo |
| 116 | + |
| 117 | + if client == nil { |
| 118 | + return clientInfo |
| 119 | + } |
| 120 | + |
| 121 | + if client.ClientId != nil { |
| 122 | + clientInfo.ClientID = *client.ClientId |
| 123 | + } |
| 124 | + if client.Scope != nil { |
| 125 | + clientInfo.Scope = *client.Scope |
| 126 | + } |
| 127 | + |
| 128 | + return clientInfo |
| 129 | +} |
| 130 | + |
| 131 | +func getHydraNextPageToken(response *http.Response, logger *slog.Logger) string { |
| 132 | + for _, linkHeader := range response.Header.Values("Link") { |
| 133 | + params := strings.Split(linkHeader, ";") |
| 134 | + link := params[0] |
| 135 | + params = params[1:] |
| 136 | + // search for rel="next" |
| 137 | + for _, param := range params { |
| 138 | + vs := strings.Split(param, "=") |
| 139 | + if len(vs) != 2 { |
| 140 | + continue |
| 141 | + } |
| 142 | + k, v := strings.TrimSpace(vs[0]), strings.TrimSpace(vs[1]) |
| 143 | + if k == "rel" && (v == "next" || v == "\"next\"") { |
| 144 | + parsedURL, err := url.Parse(link) |
| 145 | + if err != nil { |
| 146 | + logger.Warn("failed to parse url in rel=next link", "error", err, "link", linkHeader) |
| 147 | + return "" |
| 148 | + } |
| 149 | + queryParams := parsedURL.Query() |
| 150 | + for key, values := range queryParams { |
| 151 | + if key == "page_token" { |
| 152 | + return values[0] |
| 153 | + } |
| 154 | + } |
| 155 | + logger.Warn("failed to find next page token in rel=next url", "link", linkHeader) |
| 156 | + return "" |
| 157 | + } |
| 158 | + } |
| 159 | + } |
| 160 | + return "" |
| 161 | +} |
0 commit comments