User token generation in Go for the Tanker SDK.
go get github.com/TankerHQ/user-token-go/usertoken
This library depends on libsodium and libsodium-go.
As a setup example, you can check the ci/install.sh script we use to run tests in Travis.
The server-side code below demonstrates a typical flow to safely deliver user tokens to your users:
import (
"github.com/TankerHQ/user-token-go/usertoken"
)
config := usertoken.Config {
TrustchainID: "<trustchain-id>",
TrustchainPrivateKey: "<trustchain-private-key>",
}
// Example server-side function in which you would implement checkAuth(),
// retrieveUserToken() and storeUserToken() to use your own authentication
// and data storage mechanisms:
func getUserToken(string userID) (string, error) {
isAuthenticated := checkAuth(userID)
// Always ensure userID is authenticated before returning a user token
if ! isAuthenticated {
return "", error.New("Unauthorized")
}
// Retrieve a previously stored user token for this user
userToken := retrieveUserToken(userID)
// If not found, create a new user token
if userToken == "" {
userToken, err = usertoken.Generate(config, userID)
if err != nil {
return "", err
}
// Store the newly generated user token
storeUserToken(userID, userToken)
}
// From now, the same user token will always be returned to a given user
return userToken, nil
}
Read more about user tokens in the Tanker guide.
Run tests:
go test ./... -test.v
Bug reports and pull requests are welcome on GitHub at https://github.com/TankerHQ/user-token-go.