MCPcopy
hub / github.com/tinyauthapp/tinyauth / createOidcClientCmd

Function createOidcClientCmd

cmd/tinyauth/create_oidc_client.go:14–72  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

12)
13
14func createOidcClientCmd() *cli.Command {
15 return &cli.Command{
16 Name: "create",
17 Description: "Create a new OIDC Client",
18 Configuration: nil,
19 Resources: nil,
20 AllowArg: true,
21 Run: func(args []string) error {
22 if len(args) == 0 {
23 return errors.New("client name is required. use tinyauth oidc create <name>")
24 }
25
26 clientName := args[0]
27
28 match, err := regexp.MatchString("^[a-zA-Z0-9-]*$", clientName)
29
30 if !match || err != nil {
31 return errors.New("client name can only contain alphanumeric characters and hyphens")
32 }
33
34 uuid := uuid.New()
35 clientId := uuid.String()
36 clientSecret := "ta-" + utils.GenerateString(61)
37
38 uclientName := strings.ToUpper(clientName)
39 lclientName := strings.ToLower(clientName)
40
41 builder := strings.Builder{}
42
43 // header
44 fmt.Fprintf(&builder, "Created credentials for client %s\n\n", clientName)
45
46 // credentials
47 fmt.Fprintf(&builder, "Client Name: %s\n", clientName)
48 fmt.Fprintf(&builder, "Client ID: %s\n", clientId)
49 fmt.Fprintf(&builder, "Client Secret: %s\n\n", clientSecret)
50
51 // env variables
52 fmt.Fprint(&builder, "Environment variables:\n\n")
53 fmt.Fprintf(&builder, "TINYAUTH_OIDC_CLIENTS_%s_CLIENTID=%s\n", uclientName, clientId)
54 fmt.Fprintf(&builder, "TINYAUTH_OIDC_CLIENTS_%s_CLIENTSECRET=%s\n", uclientName, clientSecret)
55 fmt.Fprintf(&builder, "TINYAUTH_OIDC_CLIENTS_%s_NAME=%s\n\n", uclientName, utils.Capitalize(lclientName))
56
57 // cli flags
58 fmt.Fprint(&builder, "CLI flags:\n\n")
59 fmt.Fprintf(&builder, "--oidc.clients.%s.clientid=%s\n", lclientName, clientId)
60 fmt.Fprintf(&builder, "--oidc.clients.%s.clientsecret=%s\n", lclientName, clientSecret)
61 fmt.Fprintf(&builder, "--oidc.clients.%s.name=%s\n\n", lclientName, utils.Capitalize(lclientName))
62
63 // footer
64 fmt.Fprintln(&builder, "You can use either option to configure your OIDC client. Make sure to save these credentials as there is no way to regenerate them.")
65
66 // print
67 out := builder.String()
68 fmt.Print(out)
69 return nil
70 },
71 }

Callers 1

mainFunction · 0.85

Calls 2

GenerateStringFunction · 0.92
CapitalizeFunction · 0.92

Tested by

no test coverage detected