-
-
Notifications
You must be signed in to change notification settings - Fork 5.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
122 additions
and
10 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
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,30 @@ | ||
package sign | ||
|
||
import ( | ||
"github.com/alist-org/alist/v3/internal/setting" | ||
"github.com/alist-org/alist/v3/pkg/sign" | ||
"sync" | ||
"time" | ||
) | ||
|
||
var once sync.Once | ||
var instance sign.Sign | ||
|
||
func WithDuration(data string, d time.Duration) string { | ||
once.Do(Instance) | ||
return instance.Sign(data, time.Now().Add(d).Unix()) | ||
} | ||
|
||
func NotExpired(data string) string { | ||
once.Do(Instance) | ||
return instance.Sign(data, 0) | ||
} | ||
|
||
func Verify(data string, sign string) error { | ||
once.Do(Instance) | ||
return instance.Verify(data, sign) | ||
} | ||
|
||
func Instance() { | ||
instance = sign.NewHMACSign([]byte(setting.GetByKey("token"))) | ||
} |
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,52 @@ | ||
package sign | ||
|
||
import ( | ||
"crypto/hmac" | ||
"crypto/sha256" | ||
"encoding/base64" | ||
"io" | ||
"strconv" | ||
"strings" | ||
"time" | ||
) | ||
|
||
type HMACSign struct { | ||
SecretKey []byte | ||
} | ||
|
||
func (s HMACSign) Sign(data string, expire int64) string { | ||
h := hmac.New(sha256.New, s.SecretKey) | ||
expireTimeStamp := strconv.FormatInt(expire, 10) | ||
_, err := io.WriteString(h, data+":"+expireTimeStamp) | ||
if err != nil { | ||
return "" | ||
} | ||
|
||
return base64.URLEncoding.EncodeToString(h.Sum(nil)) + ":" + expireTimeStamp | ||
} | ||
|
||
func (s HMACSign) Verify(data, sign string) error { | ||
signSlice := strings.Split(sign, ":") | ||
// check whether contains expire time | ||
if signSlice[len(signSlice)-1] == "" { | ||
return ErrExpireMissing | ||
} | ||
// check whether expire time is expired | ||
expires, err := strconv.ParseInt(signSlice[len(signSlice)-1], 10, 64) | ||
if err != nil { | ||
return ErrExpireInvalid | ||
} | ||
// if expire time is expired, return error | ||
if expires < time.Now().Unix() && expires != 0 { | ||
return ErrSignExpired | ||
} | ||
// verify sign | ||
if s.Sign(data, expires) != sign { | ||
return ErrSignInvalid | ||
} | ||
return nil | ||
} | ||
|
||
func NewHMACSign(secret []byte) Sign { | ||
return HMACSign{SecretKey: secret} | ||
} |
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,15 @@ | ||
package sign | ||
|
||
import "errors" | ||
|
||
type Sign interface { | ||
Sign(data string, expire int64) string | ||
Verify(data, sign string) error | ||
} | ||
|
||
var ( | ||
ErrSignExpired = errors.New("sign expired") | ||
ErrSignInvalid = errors.New("sign invalid") | ||
ErrExpireInvalid = errors.New("expire invalid") | ||
ErrExpireMissing = errors.New("expire missing") | ||
) |
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