Adjust function names
This commit is contained in:
parent
b15c942e64
commit
29d8197ba3
5 changed files with 13 additions and 13 deletions
|
@ -15,7 +15,7 @@ import (
|
||||||
var rootCmd = &cobra.Command{
|
var rootCmd = &cobra.Command{
|
||||||
Use: "pye",
|
Use: "pye",
|
||||||
Short: "Pye is a simple JWT system",
|
Short: "Pye is a simple JWT system",
|
||||||
Long: `A bare-bones authentication system with RS256`,
|
Long: `An HTTP JSON Web Token authentication system`,
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
|
|
@ -23,10 +23,10 @@ var serveCmd = &cobra.Command{
|
||||||
Use: "serve",
|
Use: "serve",
|
||||||
Short: "Start JWT service",
|
Short: "Start JWT service",
|
||||||
Long: `Start a simple authentication service`,
|
Long: `Start a simple authentication service`,
|
||||||
Run: serveAuth,
|
Run: serve,
|
||||||
}
|
}
|
||||||
|
|
||||||
func serveAuth(cmd *cobra.Command, args []string) {
|
func serve(cmd *cobra.Command, args []string) {
|
||||||
if port == 0 {
|
if port == 0 {
|
||||||
port = config.Cfg.Port
|
port = config.Cfg.Port
|
||||||
}
|
}
|
||||||
|
|
|
@ -27,10 +27,10 @@ var verifyCmd = &cobra.Command{
|
||||||
Short: "Verify a JWT token",
|
Short: "Verify a JWT token",
|
||||||
Long: `Pass a JWT token (and optionally a path to a PEM-formatted file with the public key)
|
Long: `Pass a JWT token (and optionally a path to a PEM-formatted file with the public key)
|
||||||
to verify whether it is valid.`,
|
to verify whether it is valid.`,
|
||||||
Run: verifyFunc,
|
Run: verify,
|
||||||
}
|
}
|
||||||
|
|
||||||
func verifyFunc(cmd *cobra.Command, args []string) {
|
func verify(cmd *cobra.Command, args []string) {
|
||||||
if verifyToken == "" {
|
if verifyToken == "" {
|
||||||
fmt.Println("Empty token supplied!")
|
fmt.Println("Empty token supplied!")
|
||||||
return
|
return
|
||||||
|
@ -40,14 +40,14 @@ func verifyFunc(cmd *cobra.Command, args []string) {
|
||||||
var err error
|
var err error
|
||||||
if verifyFile == "" {
|
if verifyFile == "" {
|
||||||
fmt.Println("No PEM file supplied, assuming local")
|
fmt.Println("No PEM file supplied, assuming local")
|
||||||
t, err = auth.VerifyLocal(verifyToken)
|
t, err = auth.VerifyLocalToken(verifyToken)
|
||||||
} else {
|
} else {
|
||||||
key, err_k := os.ReadFile(verifyFile)
|
key, err_k := os.ReadFile(verifyFile)
|
||||||
if err_k != nil {
|
if err_k != nil {
|
||||||
slog.Error("error reading file", "error", err, "file", verifyFile)
|
slog.Error("error reading file", "error", err, "file", verifyFile)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
t, err = auth.Verify(verifyToken, key)
|
t, err = auth.VerifyToken(verifyToken, key)
|
||||||
}
|
}
|
||||||
slog.Debug("result", "token", t, "error", err, "ok", err == nil)
|
slog.Debug("result", "token", t, "error", err, "ok", err == nil)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
|
|
|
@ -15,7 +15,7 @@ func validEmail(email string) bool {
|
||||||
return err == nil
|
return err == nil
|
||||||
}
|
}
|
||||||
func validPass(pass string) bool {
|
func validPass(pass string) bool {
|
||||||
// TODO: Obviously, we *might* want something more sophisticated here
|
// Note: Obviously, we *might* want something more sophisticated here
|
||||||
return len(pass) >= 8
|
return len(pass) >= 8
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -67,7 +67,7 @@ func Login(w http.ResponseWriter, r *http.Request) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
token, err := Create(user)
|
token, err := CreateToken(user)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
http.Error(w, "error creating jwt", http.StatusInternalServerError)
|
http.Error(w, "error creating jwt", http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
|
|
|
@ -66,7 +66,7 @@ func ServePublicKey(w http.ResponseWriter, r *http.Request) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create creates a JSON Web Token that expires after a week
|
// Create creates a JSON Web Token that expires after a week
|
||||||
func Create(user user.User) (token string, err error) {
|
func CreateToken(user user.User) (token string, err error) {
|
||||||
t := jwt.NewWithClaims(jwt.SigningMethodRS256,
|
t := jwt.NewWithClaims(jwt.SigningMethodRS256,
|
||||||
jwt.MapClaims{
|
jwt.MapClaims{
|
||||||
"iss": "pye",
|
"iss": "pye",
|
||||||
|
@ -85,7 +85,7 @@ func Create(user user.User) (token string, err error) {
|
||||||
|
|
||||||
// Verify receives a JWT and PEM-encoded public key,
|
// Verify receives a JWT and PEM-encoded public key,
|
||||||
// then returns whether the token is valid
|
// then returns whether the token is valid
|
||||||
func Verify(token string, publicKey []byte) (*jwt.Token, error) {
|
func VerifyToken(token string, publicKey []byte) (*jwt.Token, error) {
|
||||||
t, err := jwt.Parse(token, func(token *jwt.Token) (interface{}, error) {
|
t, err := jwt.Parse(token, func(token *jwt.Token) (interface{}, error) {
|
||||||
key, err := jwt.ParseRSAPublicKeyFromPEM(publicKey)
|
key, err := jwt.ParseRSAPublicKeyFromPEM(publicKey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -100,8 +100,8 @@ func Verify(token string, publicKey []byte) (*jwt.Token, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// VerifyLocal calls Verify with public key set to current local one
|
// VerifyLocal calls Verify with public key set to current local one
|
||||||
func VerifyLocal(token string) (*jwt.Token, error) {
|
func VerifyLocalToken(token string) (*jwt.Token, error) {
|
||||||
key_marshalled := x509.MarshalPKCS1PublicKey(&key.PublicKey)
|
key_marshalled := x509.MarshalPKCS1PublicKey(&key.PublicKey)
|
||||||
block := pem.Block{Bytes: key_marshalled, Type: "RSA PUBLIC KEY"}
|
block := pem.Block{Bytes: key_marshalled, Type: "RSA PUBLIC KEY"}
|
||||||
return Verify(token, pem.EncodeToMemory(&block))
|
return VerifyToken(token, pem.EncodeToMemory(&block))
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue