Middle+
What are authorization tokens, and what types are there?
sobes.tech AI
Answer from AI
Authorization tokens are digital markers that confirm a user's or client's rights to access resources. They allow the server not to store session state but to verify rights via the token.
Main types of tokens:
- JWT (JSON Web Token) — a self-contained token with encrypted or signed information about the user and rights. Often used in REST API.
- OAuth tokens — used in the OAuth 2.0 protocol, include access tokens (for access) and refresh tokens (for renewing access tokens).
- Session tokens — session identifiers stored on the server in memory or database, and passed by the client in cookies.
Example of using JWT in Go:
import (
"github.com/dgrijalva/jwt-go"
"time"
)
func GenerateJWT(secret []byte) (string, error) {
claims := jwt.StandardClaims{
ExpiresAt: time.Now().Add(time.Hour).Unix(),
Issuer: "myapp",
}
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
return token.SignedString(secret)
}