1
1
package v3
2
2
3
3
import (
4
+ "crypto/sha256"
5
+ "encoding/base64"
4
6
"fmt"
5
7
"regexp"
6
8
"strconv"
@@ -12,6 +14,11 @@ import (
12
14
"github.com/gofiber/cli/cmd/internal"
13
15
)
14
16
17
+ var (
18
+ hexRe = regexp .MustCompile (`^[0-9a-fA-F]{64}$` )
19
+ b64Re = regexp .MustCompile (`^[A-Za-z0-9+/]{43}=?$` )
20
+ )
21
+
15
22
func MigrateHandlerSignatures (cmd * cobra.Command , cwd string , _ , _ * semver.Version ) error {
16
23
sigReplacer := strings .NewReplacer ("*fiber.Ctx" , "fiber.Ctx" )
17
24
@@ -715,6 +722,50 @@ func MigrateBasicauthAuthorizer(cmd *cobra.Command, cwd string, _, _ *semver.Ver
715
722
return nil
716
723
}
717
724
725
+ // MigrateBasicauthConfig adapts basicauth configuration to the new API
726
+ // * removes ContextUsername and ContextPassword fields
727
+ // * hashes plaintext Users entries using SHA-256
728
+ func MigrateBasicauthConfig (cmd * cobra.Command , cwd string , _ , _ * semver.Version ) error {
729
+ reCtxUser := regexp .MustCompile (`\s*ContextUsername:\s*[^,]+,?\n` )
730
+ reCtxPass := regexp .MustCompile (`\s*ContextPassword:\s*[^,]+,?\n` )
731
+ reUsers := regexp .MustCompile (`Users:\s*map\[string\]string{([^}]*)}` )
732
+ reEntry := regexp .MustCompile (`("[^"]+")\s*:\s*"((?:[^"\\]|\\.)*)"` )
733
+
734
+ err := internal .ChangeFileContent (cwd , func (content string ) string {
735
+ content = reCtxUser .ReplaceAllString (content , "" )
736
+ content = reCtxPass .ReplaceAllString (content , "" )
737
+
738
+ content = reUsers .ReplaceAllStringFunc (content , func (m string ) string {
739
+ sub := reUsers .FindStringSubmatch (m )
740
+ if len (sub ) < 2 {
741
+ return m
742
+ }
743
+ body := reEntry .ReplaceAllStringFunc (sub [1 ], func (s string ) string {
744
+ es := reEntry .FindStringSubmatch (s )
745
+ if len (es ) < 3 {
746
+ return s
747
+ }
748
+ pwd := es [2 ]
749
+ if strings .HasPrefix (pwd , "{" ) || strings .HasPrefix (pwd , "$2" ) || hexRe .MatchString (pwd ) || b64Re .MatchString (pwd ) {
750
+ return s
751
+ }
752
+ sum := sha256 .Sum256 ([]byte (pwd ))
753
+ hashed := base64 .StdEncoding .EncodeToString (sum [:])
754
+ return fmt .Sprintf ("%s: \" {SHA256}%s\" " , es [1 ], hashed )
755
+ })
756
+ return "Users: map[string]string{" + body + "}"
757
+ })
758
+
759
+ return content
760
+ })
761
+ if err != nil {
762
+ return fmt .Errorf ("failed to migrate basicauth config: %w" , err )
763
+ }
764
+
765
+ cmd .Println ("Migrating basicauth configs" )
766
+ return nil
767
+ }
768
+
718
769
// MigrateCacheConfig updates cache middleware configuration fields
719
770
func MigrateCacheConfig (cmd * cobra.Command , cwd string , _ , _ * semver.Version ) error {
720
771
err := internal .ChangeFileContent (cwd , func (content string ) string {
0 commit comments