Skip to content

Commit 27a3c5d

Browse files
authored
Send back the logged in LDAP username to haproxy (#27)
In case of successful login using the LDAP backend, the agent now sends an SPOE message containing the username of the logged in user. This commit also: - Updates the haproxy test config to write a X-Authorized-User header with the logged in username - Updates the nginx backend config to copy that header in a response header, to be able to test it - Updates TestShouldAuthenticateSuccessfullyInLDAP to test the new behavior
1 parent ca87a1e commit 27a3c5d

File tree

5 files changed

+15
-3
lines changed

5 files changed

+15
-3
lines changed

internal/auth/authenticator_ldap.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,5 +159,5 @@ func (la *LDAPAuthenticator) Authenticate(msg *spoe.Message) (bool, []spoe.Actio
159159
}
160160

161161
logrus.Debug("User is authenticated")
162-
return true, nil, nil
162+
return true, []spoe.Action{AuthenticatedUserMessage(username)}, nil
163163
}

internal/auth/messages.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,12 @@ func BuildHasErrorMessage() spoe.ActionSetVar {
1919
Value: true,
2020
}
2121
}
22+
23+
// AuthenticatedUserMessage build a message containing the username of the authenticated user
24+
func AuthenticatedUserMessage(username string) spoe.ActionSetVar {
25+
return spoe.ActionSetVar{
26+
Name: "authenticated_user",
27+
Scope: spoe.VarScopeSession,
28+
Value: username,
29+
}
30+
}

resources/haproxy/haproxy.cfg

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ frontend haproxynode
2828
acl oauth2logout path_beg /oauth2/logout
2929

3030
acl dex_domain hdr_beg(host) -i dex.example.com
31-
3231
# define the spoe agent
3332
filter spoe engine spoe-auth config /usr/local/etc/haproxy/spoe-auth.conf
3433

@@ -66,6 +65,7 @@ backend backend_public
6665
backend backend_app
6766
mode http
6867
balance roundrobin
68+
http-request add-header X-Authorized-User %[var(sess.auth.authenticated_user)]
6969

7070
server node-protected-app protected-backend:80 check
7171

resources/nginx/default.conf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ server {
99
location / {
1010
add_header Last-Modified $date_gmt;
1111
add_header Cache-Control 'no-store, no-cache, must-revalidate, proxy-revalidate, max-age=0';
12+
if ($http_X_Authorized_User) {
13+
add_header Request-X-Authorized-User $http_X_Authorized_User;
14+
}
1215
if_modified_since off;
1316
expires off;
1417
etag off;

tests/ldap_authentication_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ func TestShouldAuthenticateSuccessfullyInLDAP(t *testing.T) {
1414

1515
res, err := http.DefaultClient.Do(req)
1616
assert.NoError(t, err)
17-
17+
assert.Equal(t, "john", res.Header.Get("request-x-authorized-user"))
1818
assert.Equal(t, 200, res.StatusCode)
1919
}
2020

0 commit comments

Comments
 (0)