<commit_before>package tokenauth

import (
	"crypto/sha256"
	"encoding/base64"
	"errors"
	"time"
)

type MemoryTokenStore struct {
	tokens   map[string]*MemoryToken
	idTokens map[string]*MemoryToken
	salt     string
}

func (s *MemoryTokenStore) generateToken(id string) []byte {
	hash := sha256.New()
	now := time.Now()
	timeStr := now.Format(time.ANSIC)
	hash.Write([]byte(timeStr))
	hash.Write([]byte(id))
	hash.Write([]byte(s.salt))
	return hash.Sum(nil)
}

func (s *MemoryTokenStore) NewToken(id interface{}, duration int64) *MemoryToken {
	strId := id.(string)
	bToken := s.generateToken(strId)
	strToken := base64.URLEncoding.EncodeToString(bToken)
	t := &MemoryToken{
		ExpireAt: time.Now().Add((time.Duration)(duration) * time.Second),
		Token:    strToken,
		Id:       strId,
	}
	oldT, ok := s.idTokens[strId]
	if ok {
		delete(s.tokens, oldT.Token)
	}
	s.tokens[strToken] = t
	s.idTokens[strId] = t
	return t
}

func (s *MemoryTokenStore) RemoveToken(strToken string) {
	delete(s.tokens, strToken)
}

func NewMemoryTokenStore(salt string) *MemoryTokenStore {
	return &MemoryTokenStore{
		salt:     salt,
		tokens:   make(map[string]*MemoryToken),
		idTokens: make(map[string]*MemoryToken),
	}

}

func (s *MemoryTokenStore) CheckToken(strToken string) (Token, error) {
	t, ok := s.tokens[strToken]
	if !ok {
		return nil, errors.New("No this Token")
	}
	if t.ExpireAt.Before(time.Now()) {
		delete(s.tokens, strToken)
		return nil, errors.New("Token expired")
	}
	return t, nil
}<commit_msg>fix 异步<commit_after>package tokenauth

import (
	"crypto/sha256"
	"encoding/base64"
	"errors"
	"sync"
	"time"
)

var Mutex *sync.Mutex //流水号锁

type MemoryTokenStore struct {
	tokens   map[string]*MemoryToken
	idTokens map[string]*MemoryToken
	salt     string
}

func (s *MemoryTokenStore) generateToken(id string) []byte {
	hash := sha256.New()
	now := time.Now()
	timeStr := now.Format(time.ANSIC)
	hash.Write([]byte(timeStr))
	hash.Write([]byte(id))
	hash.Write([]byte(s.salt))
	return hash.Sum(nil)
}

func (s *MemoryTokenStore) NewToken(id interface{}, duration int64) *MemoryToken {
	Mutex.Lock()
	defer Mutex.Unlock()
	strId := id.(string)
	bToken := s.generateToken(strId)
	strToken := base64.URLEncoding.EncodeToString(bToken)
	t := &MemoryToken{
		ExpireAt: time.Now().Add((time.Duration)(duration) * time.Second),
		Token:    strToken,
		Id:       strId,
	}
	oldT, ok := s.idTokens[strId]
	if ok {
		delete(s.tokens, oldT.Token)
	}
	s.tokens[strToken] = t
	s.idTokens[strId] = t
	return t
}

func (s *MemoryTokenStore) RemoveToken(strToken string) {
	Mutex.Lock()
	defer Mutex.Unlock()
	delete(s.tokens, strToken)
}

func NewMemoryTokenStore(salt string) *MemoryTokenStore {
	return &MemoryTokenStore{
		salt:     salt,
		tokens:   make(map[string]*MemoryToken),
		idTokens: make(map[string]*MemoryToken),
	}

}

func (s *MemoryTokenStore) CheckToken(strToken string) (Token, error) {
	Mutex.Lock()
	defer Mutex.Unlock()
	t, ok := s.tokens[strToken]
	if !ok {
		return nil, errors.New("No this Token")
	}
	if t.ExpireAt.Before(time.Now()) {
		delete(s.tokens, strToken)
		return nil, errors.New("Token expired")
	}
	return t, nil
}

func Init() {
	Mutex = &sync.Mutex{} //流水号锁
}
{'fraction_non_alphanumeric': 0.15203761755485892, 'fraction_numerical': 0.007523510971786834, 'mean_word_length': 4.559233449477352, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3571638', 'n_tokens_mistral': 1245, 'n_tokens_neox': 1215, 'n_words': 252}