Skip to content

Commit 62d336a

Browse files
authored
feat: add custom login URL (#979)
Signed-off-by: Miguel Martinez Trivino <[email protected]>
1 parent d35a885 commit 62d336a

File tree

4 files changed

+80
-44
lines changed

4 files changed

+80
-44
lines changed

app/controlplane/internal/conf/controlplane/config/v1/conf.pb.go

Lines changed: 46 additions & 33 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

app/controlplane/internal/conf/controlplane/config/v1/conf.proto

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,9 @@ message Auth {
123123
string client_id = 2;
124124
string client_secret = 3;
125125
string redirect_url_scheme = 4;
126+
// Optional login URL that will be used by the CLI to start the OIDC flow
127+
// If not provided, it will default to [controlplane domain]/login
128+
string login_url_override = 5;
126129
}
127130

128131
message AllowList {

app/controlplane/internal/service/auth.go

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ func NewAuthService(userUC *biz.UserUseCase, orgUC *biz.OrganizationUseCase, mUC
8080
}
8181

8282
// Craft Auth related endpoints
83-
authURLs, err := getAuthURLs(serverConfig.GetHttp())
83+
authURLs, err := getAuthURLs(serverConfig.GetHttp(), authConfig.GetOidc().GetLoginUrlOverride())
8484
if err != nil {
8585
return nil, fmt.Errorf("failed to get auth URLs: %w", err)
8686
}
@@ -107,22 +107,29 @@ type AuthURLs struct {
107107
}
108108

109109
// urlScheme is deprecated, now it will be inferred from the serverConfig externalURL
110-
func getAuthURLs(serverConfig *conf.Server_HTTP) (*AuthURLs, error) {
110+
func getAuthURLs(serverConfig *conf.Server_HTTP, loginURLOverride string) (*AuthURLs, error) {
111111
host := serverConfig.Addr
112112

113-
// New mode using FQDN ExternalURL
113+
// Calculate it based on local server configuration
114+
urls := craftAuthURLs("http", host, "")
115+
116+
// If needed, use the external URL
114117
if ea := serverConfig.GetExternalUrl(); ea != "" {
115118
// x must be a valid absolute URI (via RFC 3986)
116119
url, err := url.ParseRequestURI(ea)
117120
if err != nil {
118121
return nil, fmt.Errorf("validation error: %w", err)
119122
}
120123

121-
return craftAuthURLs(url.Scheme, url.Host, url.Path), nil
124+
urls = craftAuthURLs(url.Scheme, url.Host, url.Path)
125+
}
126+
127+
// Override the login URL if needed
128+
if loginURLOverride != "" {
129+
urls.Login = loginURLOverride
122130
}
123131

124-
// Fallback no external URL
125-
return craftAuthURLs("http", host, ""), nil
132+
return urls, nil
126133
}
127134

128135
func craftAuthURLs(scheme, host, path string) *AuthURLs {

app/controlplane/internal/service/auth_test.go

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,11 @@ import (
2525
func TestGetAuthURLs(t *testing.T) {
2626
internalServer := &conf.Server_HTTP{Addr: "1.2.3.4"}
2727
testCases := []struct {
28-
name string
29-
config *conf.Server_HTTP
30-
want *AuthURLs
31-
wantErr bool
28+
name string
29+
config *conf.Server_HTTP
30+
loginURLOverride string
31+
want *AuthURLs
32+
wantErr bool
3233
}{
3334
{
3435
name: "neither external url nor externalAddr set",
@@ -60,11 +61,23 @@ func TestGetAuthURLs(t *testing.T) {
6061
config: &conf.Server_HTTP{Addr: "1.2.3.4", ExternalUrl: "localhost.com"},
6162
wantErr: true,
6263
},
64+
{
65+
name: "external with override",
66+
config: &conf.Server_HTTP{Addr: "1.2.3.4", ExternalUrl: "https://foo.com"},
67+
loginURLOverride: "https://foo.override.com/auth/login",
68+
want: &AuthURLs{callback: "https://foo.com/auth/callback", Login: "https://foo.override.com/auth/login"},
69+
},
70+
{
71+
name: "internal with override",
72+
config: internalServer,
73+
loginURLOverride: "https://foo.override.com/auth/login",
74+
want: &AuthURLs{callback: "http://1.2.3.4/auth/callback", Login: "https://foo.override.com/auth/login"},
75+
},
6376
}
6477

6578
for _, tc := range testCases {
6679
t.Run(tc.name, func(t *testing.T) {
67-
got, err := getAuthURLs(tc.config)
80+
got, err := getAuthURLs(tc.config, tc.loginURLOverride)
6881
if tc.wantErr {
6982
assert.Error(t, err)
7083
return

0 commit comments

Comments
 (0)