@@ -15,6 +15,8 @@ import (
1515 "github.com/ViBiOh/httputils/v4/pkg/renderer"
1616)
1717
18+ const authCookieName = "_auth"
19+
1820type 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+
117137func (s Service ) parseShare (ctx context.Context , request * provider.Request , password string ) error {
118138 share := s .share .Get (request .Filepath ())
119139 if share .IsZero () {
0 commit comments