-
Notifications
You must be signed in to change notification settings - Fork 149
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for proxy-based authentication
- Loading branch information
Showing
6 changed files
with
124 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
Enhancement: Add support for proxy-based authentication | ||
|
||
The server now supports authentication via a proxy header specified with the --proxy-auth flag (e.g., --proxy-auth=X-Forwarded-User). | ||
When this flag is set, the server will authenticate users based on the given header and disable BasicAuth. | ||
Note that --proxy-auth is ignored if --no-auth is set, as --no-auth disables all authentication. | ||
|
||
https://github.com/restic/rest-server/issues/174 | ||
https://github.com/restic/rest-server/pull/307 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package restserver | ||
|
||
import ( | ||
"net/http/httptest" | ||
"testing" | ||
) | ||
|
||
func TestCheckAuth(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
server *Server | ||
requestHeaders map[string]string | ||
basicAuth bool | ||
basicUser string | ||
basicPassword string | ||
expectedUser string | ||
expectedOk bool | ||
}{ | ||
{ | ||
name: "NoAuth enabled", | ||
server: &Server{ | ||
NoAuth: true, | ||
}, | ||
expectedOk: true, | ||
}, | ||
{ | ||
name: "Proxy Auth successful", | ||
server: &Server{ | ||
ProxyAuthUsername: "X-Remote-User", | ||
}, | ||
requestHeaders: map[string]string{ | ||
"X-Remote-User": "restic", | ||
}, | ||
expectedUser: "restic", | ||
expectedOk: true, | ||
}, | ||
{ | ||
name: "Proxy Auth empty header", | ||
server: &Server{ | ||
ProxyAuthUsername: "X-Remote-User", | ||
}, | ||
requestHeaders: map[string]string{ | ||
"X-Remote-User": "", | ||
}, | ||
expectedOk: false, | ||
}, | ||
{ | ||
name: "Proxy Auth missing header", | ||
server: &Server{ | ||
ProxyAuthUsername: "X-Remote-User", | ||
}, | ||
expectedOk: false, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
req := httptest.NewRequest("GET", "/", nil) | ||
for header, value := range tt.requestHeaders { | ||
req.Header.Set(header, value) | ||
} | ||
if tt.basicAuth { | ||
req.SetBasicAuth(tt.basicUser, tt.basicPassword) | ||
} | ||
|
||
username, ok := tt.server.checkAuth(req) | ||
if username != tt.expectedUser || ok != tt.expectedOk { | ||
t.Errorf("expected (%v, %v), got (%v, %v)", tt.expectedUser, tt.expectedOk, username, ok) | ||
} | ||
}) | ||
} | ||
} |