oc-auth/models/registration.go

138 lines
4.5 KiB
Go
Raw Normal View History

2024-09-27 09:20:59 +02:00
package models
import (
"context"
"fmt"
"os"
client "github.com/ory/hydra-client-go"
"golang.org/x/oauth2"
"golang.org/x/oauth2/clientcredentials"
)
func CreateClient(clientId string, clientName string, clientSecret string) (string, string, error) {
tokenAuthMethod := "client_secret_post"
oAuth2Client := *client.NewOAuth2Client() // OAuth2Client |
oAuth2Client.SetClientId(clientId)
oAuth2Client.SetClientName(clientName)
oAuth2Client.SetClientSecret(clientSecret)
oAuth2Client.SetGrantTypes([]string{"client_credentials"})
oAuth2Client.TokenEndpointAuthMethod = &tokenAuthMethod
config := client.NewConfiguration()
config.Servers = client.ServerConfigurations{{URL: "http://127.0.0.1:4445"}}
client := client.NewAPIClient(config)
resp, _, err := client.AdminApi.CreateOAuth2Client(context.Background()).OAuth2Client(oAuth2Client).Execute()
if err != nil {
return "", "", err
}
return resp.GetClientId(), resp.GetClientSecret(), nil
}
func GetAccessToken(clientID, clientSecret string) (string, error) {
config := clientcredentials.Config{
ClientID: clientID,
ClientSecret: clientSecret,
TokenURL: "http://127.0.0.1:4444/oauth2/token",
AuthStyle: oauth2.AuthStyleInParams,
}
token, err := config.Token(context.Background())
if err != nil {
fmt.Println("Error obtaining token:", err)
return "", err
}
return token.AccessToken, nil
}
func ListClients() {
configuration := client.NewConfiguration()
configuration.Servers = []client.ServerConfiguration{
{
URL: "http://localhost:4445", // Public API URL
},
}
apiClient := client.NewAPIClient(configuration)
limit := int64(20)
offset := int64(0)
clients, r, err := apiClient.AdminApi.ListOAuth2Clients(context.Background()).Limit(limit).Offset(offset).Execute()
if err != nil {
fmt.Fprintf(os.Stderr, "Error when calling `OAuth2Api.ListOAuth2Clients``: %v\n", err)
fmt.Fprintf(os.Stderr, "Full HTTP response: %v\n", r)
}
for i, c := range clients {
fmt.Fprintf(os.Stdout, " %d : %s %s %s\n", i, *c.ClientId, c.GetClientName(), c.GetClientSecret())
}
fmt.Fprintf(os.Stdout, "We have %d clients\n", len(clients))
}
func DeleteClient(clientId string) {
configuration := client.NewConfiguration()
configuration.Servers = []client.ServerConfiguration{
{
URL: "http://localhost:4445", // Public API URL
},
}
apiClient := client.NewAPIClient(configuration)
r, err := apiClient.AdminApi.DeleteOAuth2Client(context.Background(), clientId).Execute()
if err != nil {
fmt.Fprintf(os.Stderr, "Error when calling `OAuth2Api.DeleteOAuth2Client``: %v\n", err)
fmt.Fprintf(os.Stderr, "Full HTTP response: %v\n", r)
}
}
func DeleteClients() {
configuration := client.NewConfiguration()
configuration.Servers = []client.ServerConfiguration{
{
URL: "http://localhost:4445", // Public API URL
},
}
apiClient := client.NewAPIClient(configuration)
limit := int64(20)
offset := int64(0)
clients, r, err := apiClient.AdminApi.ListOAuth2Clients(context.Background()).Limit(limit).Offset(offset).Execute()
if err != nil {
fmt.Fprintf(os.Stderr, "Error when calling `OAuth2Api.ListOAuth2Clients``: %v\n", err)
fmt.Fprintf(os.Stderr, "Full HTTP response: %v\n", r)
}
for _, c := range clients {
fmt.Fprintf(os.Stdout, " Deleting : %s %s %s\n", c.GetClientId(), c.GetClientName(), c.GetClientSecret())
r, err := apiClient.AdminApi.DeleteOAuth2Client(context.Background(), c.GetClientId()).Execute()
if err != nil {
fmt.Fprintf(os.Stderr, "Error when calling `OAuth2Api.DeleteOAuth2Client``: %v\n", err)
fmt.Fprintf(os.Stderr, "Full HTTP response: %v\n", r)
}
fmt.Fprintf(os.Stdout, " Deleted: %s\n", c.GetClientId())
}
fmt.Fprintf(os.Stdout, "We deleted %d clients\n", len(clients))
}
func CreateCodeClient(clientId string) (string, string, error) {
config := client.NewConfiguration()
config.Servers = client.ServerConfigurations{{URL: "http://127.0.0.1:4445"}}
tokenAuthMethod := "client_secret_post"
oAuth2Client := *client.NewOAuth2Client() // OAuth2Client |
oAuth2Client.SetClientId(clientId + "_api")
oAuth2Client.SetGrantTypes([]string{"authorization_code", "refresh_token"})
oAuth2Client.SetResponseTypes([]string{"code", "id_token"})
oAuth2Client.SetScope("openid offline")
oAuth2Client.SetRedirectUris([]string{"http://127.0.0.1:5555/callback"})
oAuth2Client.TokenEndpointAuthMethod = &tokenAuthMethod
client := client.NewAPIClient(config)
resp, _, err := client.AdminApi.CreateOAuth2Client(context.Background()).OAuth2Client(oAuth2Client).Execute()
if err != nil {
return "", "", err
}
return resp.GetClientId(), resp.GetClientSecret(), nil
}