Skip to content

Commit c60b0a0

Browse files
committed
feat: Adding cookie support for basic auth
Signed-off-by: Vincent Boutour <[email protected]>
1 parent f5067ff commit c60b0a0

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

pkg/fibr/fibr.go

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import (
1515
"github.com/ViBiOh/httputils/v4/pkg/renderer"
1616
)
1717

18+
const authCookieName = "_auth"
19+
1820
type Service struct {
1921
login provider.Auth
2022
crud provider.Crud
@@ -35,7 +37,7 @@ func New(crud provider.Crud, renderer *renderer.Service, share provider.ShareMan
3537
}
3638
}
3739

38-
func (s Service) parseRequest(r *http.Request) (provider.Request, error) {
40+
func (s Service) parseRequest(w http.ResponseWriter, r *http.Request) (provider.Request, error) {
3941
ctx := r.Context()
4042

4143
request := provider.Request{
@@ -54,7 +56,7 @@ func (s Service) parseRequest(r *http.Request) (provider.Request, error) {
5456
request.Path = "/" + request.Path
5557
}
5658

57-
login, password, basicOK := r.BasicAuth()
59+
login, password, basicOK, shouldUpdateCookie := s.getCredentials(r)
5860

5961
if err := s.parseShare(ctx, &request, password); err != nil {
6062
return request, model.WrapUnauthorized(err)
@@ -95,6 +97,10 @@ func (s Service) parseRequest(r *http.Request) (provider.Request, error) {
9597
return request, convertAuthenticationError(err)
9698
}
9799

100+
if shouldUpdateCookie {
101+
s.cookie.Set(ctx, w, authCookieName, login, password)
102+
}
103+
98104
if s.login.IsAuthorized(ctx, user, "admin") {
99105
request.CanEdit = true
100106
request.CanShare = true
@@ -114,6 +120,20 @@ func parsePreferences(r *http.Request) provider.Preferences {
114120
return provider.ParsePreferences(cookieValue)
115121
}
116122

123+
func (s Service) getCredentials(r *http.Request) (string, string, bool, bool) {
124+
login, password, ok := r.BasicAuth()
125+
if ok {
126+
return login, password, ok, true
127+
}
128+
129+
claim, err := s.cookie.Get(r, authCookieName)
130+
if err != nil {
131+
return login, password, ok, false
132+
}
133+
134+
return claim.Login, claim.Password, true, false
135+
}
136+
117137
func (s Service) parseShare(ctx context.Context, request *provider.Request, password string) error {
118138
share := s.share.Get(request.Filepath())
119139
if share.IsZero() {

pkg/fibr/fibr_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ func TestParseRequest(t *testing.T) {
413413
loginMock.EXPECT().IsAuthorized(gomock.Any(), gomock.Any(), gomock.Any()).Return(true)
414414
}
415415

416-
got, gotErr := tc.instance.parseRequest(tc.args.r)
416+
got, gotErr := tc.instance.parseRequest(httptest.NewRecorder(), tc.args.r)
417417

418418
failed := false
419419

pkg/fibr/renderer.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ func (s Service) TemplateFunc(w http.ResponseWriter, r *http.Request) (renderer.
7979
return renderer.Page{}, nil
8080
}
8181

82-
request, err := s.parseRequest(r)
82+
request, err := s.parseRequest(w, r)
8383
if err != nil {
8484
if errors.Is(err, model.ErrUnauthorized) {
8585
w.Header().Add("WWW-Authenticate", `Basic realm="fibr" charset="UTF-8"`)

0 commit comments

Comments
 (0)