2024-01-04 16:21:40 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
peering "forge.cadoles.com/Cadoles/go-http-peering"
|
|
|
|
"github.com/dgrijalva/jwt-go"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
func verifyToken() {
|
|
|
|
rawToken, err := loadToken()
|
|
|
|
if err != nil {
|
|
|
|
handleError(errors.WithStack(err))
|
|
|
|
}
|
|
|
|
|
|
|
|
privateKey, err := loadPrivateKey()
|
|
|
|
if err != nil {
|
|
|
|
handleError(errors.WithStack(err))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn := func(token *jwt.Token) (interface{}, error) {
|
|
|
|
return &privateKey.PublicKey, nil
|
|
|
|
}
|
|
|
|
|
2024-01-05 11:12:27 +01:00
|
|
|
token, err := jwt.ParseWithClaims(rawToken, &peering.ServerTokenClaims{}, fn)
|
2024-01-04 16:21:40 +01:00
|
|
|
if err != nil {
|
|
|
|
validationError, ok := err.(*jwt.ValidationError)
|
|
|
|
if ok {
|
|
|
|
handleError(errors.WithStack(validationError.Inner))
|
|
|
|
}
|
|
|
|
|
|
|
|
handleError(errors.WithStack(err))
|
|
|
|
}
|
|
|
|
|
|
|
|
if !token.Valid {
|
|
|
|
handleError(errors.New("token is invalid"))
|
|
|
|
}
|
|
|
|
|
|
|
|
claims, ok := token.Claims.(*peering.ServerTokenClaims)
|
|
|
|
if !ok {
|
|
|
|
handleError(errors.New("unexpected token claims"))
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := json.MarshalIndent(claims, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
handleError(errors.WithStack(err))
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println("Token OK.")
|
|
|
|
fmt.Println("Claims:")
|
|
|
|
fmt.Println(string(data))
|
|
|
|
|
|
|
|
}
|