Skip to content

Instantly share code, notes, and snippets.

@cbarraford
Last active March 2, 2019 16:46
Show Gist options
  • Save cbarraford/c99b234269dbba08bf6c55d8b1e9796f to your computer and use it in GitHub Desktop.
Save cbarraford/c99b234269dbba08bf6c55d8b1e9796f to your computer and use it in GitHub Desktop.
Timebased token creation for
package token
import (
"crypto/sha256"
"fmt"
"os"
"time"
"github.com/cbarraford/octagon-ql/store"
)
// get our salt from the env var
var salt = os.Getenv("TOKEN_SALT")
func GenerateToken(ts int64, user store.User) string {
str := fmt.Sprintf("%d-%d-%s-%s", ts, user.ID, user.Password, salt)
sha := sha256.Sum256([]byte(str))
return string(sha[:])
}
func ValidateToken(ts int64, token string, user store.User) bool {
now := time.Now().Unix()
// check if ts is older than a day
if now-ts > 86400 {
return false
}
// check ts is NOT in the future
if now < ts {
return false
}
return GenerateStudentToken(ts, student) == token
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment