Send With Confidence
Partner with the email service trusted by developers and marketers for time-savings, scalability, and delivery expertise.
Time to read: 1 minute
$ openssl genrsa -out demo.rsa 1024 # the 1024 is the size of the key we are generating | |
$ openssl rsa -in demo.rsa -pubout > demo.rsa.pub |
import ( | |
jwt "github.com/dgrijalva/jwt-go" | |
"io/ioutil" | |
"net/http" | |
"fmt" | |
) | |
var ( | |
privateKey []byte | |
) | |
func init() { | |
privateKey, _ = ioutil.ReadFile("Location of your demo.rsa") | |
} | |
func handleRequest(w http.ResponseWriter, r *http.Request) { | |
token := jwt.New(jwt.GetSigningMethod("RS256")) // Create a Token that will be signed with RSA 256. | |
/* | |
{ | |
"typ":"JWT", | |
"alg":"RS256" | |
} | |
/* | |
token.Claims["ID"] = "This is my super fake ID" | |
token.Claims["exp"] = time.Now().Unix() + 36000 | |
// The claims object allows you to store information in the actual token. | |
tokenString, _ := token.SignedString(privateKey) | |
// tokenString Contains the actual token you should share with your client. | |
w.WriteHeader(http.StatusOK) | |
fmt.Fprintf(w, "{\"token\": %s}", tokenString) | |
} | |
import ( | |
jwt "github.com/dgrijalva/jwt-go" | |
"io/ioutil" | |
"net/http" | |
"fmt" | |
) | |
var ( | |
publicKey []byte | |
) | |
func init() { | |
publicKey, _ = ioutil.ReadFile("Location of your demo.rsa.pub") | |
} | |
func authRequest(w http.ResponseWriter, r *http.Request) { | |
token, err := jwt.ParseFromRequest(r, func(token *jwt.Token) ([]byte, error) { | |
return publicKey, nil | |
}) | |
if token.Valid { | |
//YAY! | |
} else { | |
//Someone is being funny | |
} | |
} | |
Partner with the email service trusted by developers and marketers for time-savings, scalability, and delivery expertise.