@@ -91,13 +91,14 @@ func Start() {
91
91
}
92
92
loadExpiryImage ()
93
93
94
- mux .HandleFunc ("/admin" , requireLogin (showAdminMenu , false ))
94
+ mux .HandleFunc ("/admin" , requireLogin (showAdminMenu , true , false ))
95
95
mux .HandleFunc ("/api/" , processApi )
96
- mux .HandleFunc ("/apiKeys" , requireLogin (showApiAdmin , false ))
96
+ mux .HandleFunc ("/apiKeys" , requireLogin (showApiAdmin , true , false ))
97
+ mux .HandleFunc ("/changePassword" , requireLogin (changePassword , true , true ))
97
98
mux .HandleFunc ("/d" , showDownload )
98
99
mux .HandleFunc ("/downloadFile" , downloadFile )
99
- mux .HandleFunc ("/e2eInfo" , requireLogin (e2eInfo , true ))
100
- mux .HandleFunc ("/e2eSetup" , requireLogin (showE2ESetup , false ))
100
+ mux .HandleFunc ("/e2eInfo" , requireLogin (e2eInfo , true , false ))
101
+ mux .HandleFunc ("/e2eSetup" , requireLogin (showE2ESetup , true , false ))
101
102
mux .HandleFunc ("/error" , showError )
102
103
mux .HandleFunc ("/error-auth" , showErrorAuth )
103
104
mux .HandleFunc ("/error-header" , showErrorHeader )
@@ -106,12 +107,12 @@ func Start() {
106
107
mux .HandleFunc ("/hotlink/" , showHotlink )
107
108
mux .HandleFunc ("/index" , showIndex )
108
109
mux .HandleFunc ("/login" , showLogin )
109
- mux .HandleFunc ("/logs" , requireLogin (showLogs , false ))
110
+ mux .HandleFunc ("/logs" , requireLogin (showLogs , true , false ))
110
111
mux .HandleFunc ("/logout" , doLogout )
111
- mux .HandleFunc ("/uploadChunk" , requireLogin (uploadChunk , true ))
112
- mux .HandleFunc ("/uploadComplete" , requireLogin (uploadComplete , true ))
113
- mux .HandleFunc ("/uploadStatus" , requireLogin (sse .GetStatusSSE , true ))
114
- mux .HandleFunc ("/users" , requireLogin (showUserAdmin , false ))
112
+ mux .HandleFunc ("/uploadChunk" , requireLogin (uploadChunk , true , false ))
113
+ mux .HandleFunc ("/uploadComplete" , requireLogin (uploadComplete , true , false ))
114
+ mux .HandleFunc ("/uploadStatus" , requireLogin (sse .GetStatusSSE , true , false ))
115
+ mux .HandleFunc ("/users" , requireLogin (showUserAdmin , true , false ))
115
116
mux .Handle ("/main.wasm" , gziphandler .GzipHandler (http .HandlerFunc (serveDownloadWasm )))
116
117
mux .Handle ("/e2e.wasm" , gziphandler .GzipHandler (http .HandlerFunc (serveE2EWasm )))
117
118
@@ -256,6 +257,57 @@ func showIndex(w http.ResponseWriter, r *http.Request) {
256
257
helper .CheckIgnoreTimeout (err )
257
258
}
258
259
260
+ // Handling of /changePassword
261
+ func changePassword (w http.ResponseWriter , r * http.Request ) {
262
+ var errMessage string
263
+ user , err := authentication .GetUserFromRequest (r )
264
+ if err != nil {
265
+ panic (err )
266
+ }
267
+ if ! user .ResetPassword {
268
+ redirect (w , "admin" )
269
+ return
270
+ }
271
+ err = r .ParseForm ()
272
+ if err != nil {
273
+ fmt .Println ("Invalid form data sent to server for /changePassword" )
274
+ fmt .Println (err )
275
+ errMessage = "Invalid form data sent"
276
+ } else {
277
+ var ok bool
278
+ var pwHash string
279
+
280
+ pw := r .Form .Get ("newpw" )
281
+ errMessage , pwHash , ok = validateNewPassword (pw , user )
282
+ if ok {
283
+ user .Password = pwHash
284
+ user .ResetPassword = false
285
+ database .SaveUser (user , false )
286
+ redirect (w , "admin" )
287
+ return
288
+ }
289
+ }
290
+ err = templateFolder .ExecuteTemplate (w , "changepw" ,
291
+ genericView {PublicName : configuration .Get ().PublicName ,
292
+ MinPasswordLength : configuration .MinLengthPassword ,
293
+ ErrorMessage : errMessage })
294
+ helper .CheckIgnoreTimeout (err )
295
+ }
296
+
297
+ func validateNewPassword (newPassword string , user models.User ) (string , string , bool ) {
298
+ if len (newPassword ) == 0 {
299
+ return "" , user .Password , false
300
+ }
301
+ if len (newPassword ) < configuration .MinLengthPassword {
302
+ return "Password is too short" , user .Password , false
303
+ }
304
+ newPasswordHash := configuration .HashPassword (newPassword , false )
305
+ if user .Password == newPasswordHash {
306
+ return "New password has to be different from the old password" , user .Password , false
307
+ }
308
+ return "" , newPasswordHash , true
309
+ }
310
+
259
311
// Handling of /error
260
312
func showError (w http.ResponseWriter , r * http.Request ) {
261
313
const invalidFile = 0
@@ -305,7 +357,7 @@ func forgotPassword(w http.ResponseWriter, r *http.Request) {
305
357
// Handling of /api
306
358
// If user is authenticated, this menu lists all uploads and enables uploading new files
307
359
func showApiAdmin (w http.ResponseWriter , r * http.Request ) {
308
- userId , err := authentication .GetUserIdFromRequest (r )
360
+ userId , err := authentication .GetUserFromRequest (r )
309
361
if err != nil {
310
362
panic (err )
311
363
}
@@ -317,7 +369,7 @@ func showApiAdmin(w http.ResponseWriter, r *http.Request) {
317
369
// Handling of /users
318
370
// If user is authenticated, this menu lists all users
319
371
func showUserAdmin (w http.ResponseWriter , r * http.Request ) {
320
- userId , err := authentication .GetUserIdFromRequest (r )
372
+ userId , err := authentication .GetUserFromRequest (r )
321
373
if err != nil {
322
374
panic (err )
323
375
}
@@ -480,16 +532,16 @@ func e2eInfo(w http.ResponseWriter, r *http.Request) {
480
532
return
481
533
}
482
534
483
- userId , err := authentication .GetUserIdFromRequest (r )
535
+ user , err := authentication .GetUserFromRequest (r )
484
536
if err != nil {
485
537
responseError (w , err )
486
538
return
487
539
}
488
540
switch action [0 ] {
489
541
case "get" :
490
- getE2eInfo (w , userId )
542
+ getE2eInfo (w , user . Id )
491
543
case "store" :
492
- storeE2eInfo (w , r , userId )
544
+ storeE2eInfo (w , r , user . Id )
493
545
default :
494
546
responseError (w , errors .New ("invalid action specified" ))
495
547
}
@@ -546,26 +598,26 @@ func queryUrl(w http.ResponseWriter, r *http.Request, redirectUrl string) string
546
598
// If user is authenticated, this menu lists all uploads and enables uploading new files
547
599
func showAdminMenu (w http.ResponseWriter , r * http.Request ) {
548
600
549
- userId , err := authentication .GetUserIdFromRequest (r )
601
+ user , err := authentication .GetUserFromRequest (r )
550
602
if err != nil {
551
603
panic (err )
552
604
}
553
605
554
606
if configuration .Get ().Encryption .Level == encryption .EndToEndEncryption {
555
- e2einfo := database .GetEnd2EndInfo (userId )
607
+ e2einfo := database .GetEnd2EndInfo (user . Id )
556
608
if ! e2einfo .HasBeenSetUp () {
557
609
redirect (w , "e2eSetup" )
558
610
return
559
611
}
560
612
}
561
- err = templateFolder .ExecuteTemplate (w , "admin" , (& UploadView {}).convertGlobalConfig (ViewMain , userId ))
613
+ err = templateFolder .ExecuteTemplate (w , "admin" , (& UploadView {}).convertGlobalConfig (ViewMain , user ))
562
614
helper .CheckIgnoreTimeout (err )
563
615
}
564
616
565
617
// Handling of /logs
566
618
// If user is authenticated, this menu shows the stored logs
567
619
func showLogs (w http.ResponseWriter , r * http.Request ) {
568
- userId , err := authentication .GetUserIdFromRequest (r )
620
+ userId , err := authentication .GetUserFromRequest (r )
569
621
if err != nil {
570
622
panic (err )
571
623
}
@@ -584,11 +636,11 @@ func showE2ESetup(w http.ResponseWriter, r *http.Request) {
584
636
return
585
637
}
586
638
587
- userId , err := authentication .GetUserIdFromRequest (r )
639
+ user , err := authentication .GetUserFromRequest (r )
588
640
if err != nil {
589
641
panic (err )
590
642
}
591
- e2einfo := database .GetEnd2EndInfo (userId )
643
+ e2einfo := database .GetEnd2EndInfo (user . Id )
592
644
err = templateFolder .ExecuteTemplate (w , "e2esetup" , e2ESetupView {HasBeenSetup : e2einfo .HasBeenSetUp (), PublicName : configuration .Get ().PublicName })
593
645
helper .CheckIgnoreTimeout (err )
594
646
}
@@ -635,6 +687,7 @@ type UploadView struct {
635
687
IsUserTabAvailable bool
636
688
EndToEndEncryption bool
637
689
IncludeFilename bool
690
+ IsInternalAuth bool
638
691
MaxFileSize int
639
692
ActiveView int
640
693
ChunkSize int
@@ -666,20 +719,20 @@ const (
666
719
667
720
// Converts the globalConfig variable to an UploadView struct to pass the infos to
668
721
// the admin template
669
- func (u * UploadView ) convertGlobalConfig (view , userId int ) * UploadView {
722
+ func (u * UploadView ) convertGlobalConfig (view int , user models. User ) * UploadView {
670
723
var result []models.FileApiOutput
671
724
var resultApi []models.ApiKey
672
725
673
- user , ok := database .GetUser (userId )
674
- if ! ok {
675
- panic ("user not found" )
676
- }
726
+ config := configuration .Get ()
727
+ u .IsInternalAuth = config .Authentication .Method == models .AuthenticationInternal
677
728
u .ActiveUser = user
678
729
u .UserMap = getUserMap ()
679
- config := configuration .Get ()
680
730
switch view {
681
731
case ViewMain :
682
732
for _ , element := range database .GetAllMetadata () {
733
+ if element .UserId != user .Id && ! user .HasPermissionListOtherUploads () {
734
+ continue
735
+ }
683
736
fileInfo , err := element .ToFileApiOutput (config .ServerUrl , config .IncludeFilename )
684
737
helper .Check (err )
685
738
result = append (result , fileInfo )
@@ -695,7 +748,7 @@ func (u *UploadView) convertGlobalConfig(view, userId int) *UploadView {
695
748
// Double-checking if user of API key exists
696
749
// If the user was manually deleted from the database, this could lead to a crash
697
750
// in the API view
698
- _ , ok = u .UserMap [apiKey .UserId ]
751
+ _ , ok : = u .UserMap [apiKey .UserId ]
699
752
if ! ok {
700
753
continue
701
754
}
@@ -728,7 +781,7 @@ func (u *UploadView) convertGlobalConfig(view, userId int) *UploadView {
728
781
User : userEntry ,
729
782
}
730
783
// Otherwise the user is not shown as online, if /users is opened as first page
731
- if userEntry .Id == userId {
784
+ if userEntry .Id == user . Id {
732
785
userWithUploads .User .LastOnline = time .Now ().Unix ()
733
786
}
734
787
u .Users = append (u .Users , userWithUploads )
@@ -749,7 +802,7 @@ func (u *UploadView) convertGlobalConfig(view, userId int) *UploadView {
749
802
u .MaxParallelUploads = config .MaxParallelUploads
750
803
u .ChunkSize = config .ChunkSize
751
804
u .IncludeFilename = config .IncludeFilename
752
- u .SystemKey = api .GetSystemKey (userId )
805
+ u .SystemKey = api .GetSystemKey (user . Id )
753
806
return u
754
807
}
755
808
@@ -780,12 +833,12 @@ func uploadComplete(w http.ResponseWriter, r *http.Request) {
780
833
responseError (w , err )
781
834
return
782
835
}
783
- userId , err := authentication .GetUserIdFromRequest (r )
836
+ user , err := authentication .GetUserFromRequest (r )
784
837
if err != nil {
785
838
panic (err )
786
839
}
787
840
go func () {
788
- _ , err = fileupload .CompleteChunk (chunkId , header , userId , config )
841
+ _ , err = fileupload .CompleteChunk (chunkId , header , user . Id , config )
789
842
if err != nil {
790
843
processingstatus .Set (chunkId , processingstatus .StatusError , models.File {}, err )
791
844
fmt .Println (err )
@@ -843,17 +896,23 @@ func serveFile(id string, isRootUrl bool, w http.ResponseWriter, r *http.Request
843
896
storage .ServeFile (savedFile , w , r , true )
844
897
}
845
898
846
- func requireLogin (next http.HandlerFunc , isUpload bool ) http.HandlerFunc {
899
+ func requireLogin (next http.HandlerFunc , isUiCall , isPwChangeView bool ) http.HandlerFunc {
847
900
return func (w http.ResponseWriter , r * http.Request ) {
848
901
addNoCacheHeader (w )
849
- isLoggedIn , userId := authentication .IsAuthenticated (w , r )
902
+ isLoggedIn , user := authentication .IsAuthenticated (w , r )
850
903
if isLoggedIn {
851
- c := context .WithValue (r .Context (), "userId" , userId )
904
+ if user .ResetPassword && isUiCall && configuration .Get ().Authentication .Method == models .AuthenticationInternal {
905
+ if ! isPwChangeView {
906
+ redirect (w , "changePassword" )
907
+ return
908
+ }
909
+ }
910
+ c := context .WithValue (r .Context (), "user" , user )
852
911
r = r .WithContext (c )
853
912
next .ServeHTTP (w , r )
854
913
return
855
914
}
856
- if isUpload {
915
+ if ! isUiCall {
857
916
w .WriteHeader (http .StatusUnauthorized )
858
917
_ , _ = io .WriteString (w , "{\" Result\" :\" error\" ,\" ErrorMessage\" :\" Not authenticated\" }" )
859
918
return
@@ -893,11 +952,13 @@ func addNoCacheHeader(w http.ResponseWriter) {
893
952
894
953
// A view containing parameters for a generic template
895
954
type genericView struct {
896
- IsAdminView bool
897
- IsDownloadView bool
898
- PublicName string
899
- RedirectUrl string
900
- ErrorId int
955
+ IsAdminView bool
956
+ IsDownloadView bool
957
+ PublicName string
958
+ RedirectUrl string
959
+ ErrorMessage string
960
+ ErrorId int
961
+ MinPasswordLength int
901
962
}
902
963
903
964
// A view containing parameters for an oauth error
0 commit comments