bumpy-controller-43157
01/14/2025, 11:52 AMsteep-lamp-91158
func (s *Service) UpsertOAuth2Client(ctx context.Context, cl hydra.OAuth2Client) (*hydra.OAuth2Client, error) {
// Try update first
c, _, err := s.hc.OAuth2API.SetOAuth2Client(ctx, *cl.ClientId).OAuth2Client(cl).Execute()
if err != nil {
// If not found, create
c, _, err = s.hc.OAuth2API.CreateOAuth2Client(ctx).OAuth2Client(cl).Execute()
return c, err
}
return c, nil
}
steep-lamp-91158
bumpy-controller-43157
01/14/2025, 1:25 PMsteep-lamp-91158
bumpy-controller-43157
01/14/2025, 2:39 PMbumpy-controller-43157
01/14/2025, 2:39 PM#!/usr/bin/env bash
client_file="$1" # references a json file describing the client
new_client_name=$(jq -r '.client_name' < "$client_file")
echo "new client name: $new_client_name"
output=$(ory list oauth2-clients --format json-pretty)
# used to check if a client_id already exists for said client name
client_id=$(echo "$output" | jq -r --arg new_client_name "$new_client_name" '.items[] | select(.client_name == $new_client_name) | .client_id')
if [ -n "$client_id" ]; then
echo "Client '$new_client_name' already exists."
echo "Client id: $client_id"
ory update oauth2-client "$client_id" --file "$client_file"
else
echo "Client '$new_client_name' does not exist yet."
ory create oauth2-client --file "$client_file"
fi