Skip to content

Commit

Permalink
Merge pull request #22 from edwinvautier/feat_install-command
Browse files Browse the repository at this point in the history
Feat: Add fake bundle to test importing it.
  • Loading branch information
Edwin Vautier authored Mar 30, 2021
2 parents 9a95e71 + f167a51 commit 4494c6a
Show file tree
Hide file tree
Showing 6 changed files with 176 additions and 39 deletions.
19 changes: 19 additions & 0 deletions bundles/authenticator/authenticator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package authenticator

import (
"github.com/dgrijalva/jwt-go"
)
type authenticatorInterface interface {
GenerateToken(string) (error, string)
DecodeToken(string)
HashPassword(string, string) error
}

// Claim is the struct for the jwt claim
type Claim struct {
Email string
jwt.StandardClaims
}

// Authenticator is the struct for the authenticator bundle
type Authenticator struct {}
26 changes: 26 additions & 0 deletions bundles/authenticator/env.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package authenticator

import (
"os"

"github.com/joho/godotenv"
log "github.com/sirupsen/logrus"
)

// GoDotEnvVariable loads a variable from the .env file
func goDotEnvVariable(key string) string {

// load .env file
err := godotenv.Load(".env")

if err != nil {
log.Error("Error loading .env file", err)
}

envVariable, variableExists := os.LookupEnv(key)
if !variableExists {
log.Error("Couldn't find variable : ", key)
}

return envVariable
}
16 changes: 16 additions & 0 deletions bundles/authenticator/password_hasher.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package authenticator

import (
"crypto/sha512"
"fmt"
)

// HashPassword takes a string in parameter and returns the same string hashed with sha512
func (auth Authenticator) HashPassword(password string) string {
h := sha512.New()
h.Write([]byte(password))
bytesHash := h.Sum(nil)
hexString := fmt.Sprintf("%x", bytesHash)
// Returns the hexa string
return hexString
}
108 changes: 108 additions & 0 deletions bundles/authenticator/token.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package authenticator

import (
"errors"
"io/ioutil"
"strconv"
"time"

"github.com/dgrijalva/jwt-go"
log "github.com/sirupsen/logrus"
)

// Rsa is the struct to get the rsa keys used to generate and verify tokens from the environment variables
type Rsa struct {
PublicKeyPath string
PrivateKeyPath string
PublicKey interface{}
PrivateKey interface{}
}

var rsa Rsa

func initRsaKeys() error {
if rsa.PrivateKey != nil && rsa.PublicKey != nil {
return nil
}

rsa.PublicKeyPath = goDotEnvVariable("RSA_PUBLIC_PATH")
rsa.PrivateKeyPath = goDotEnvVariable("RSA_PRIVATE_PATH")

// Get the public key
publicKeyData, err := ioutil.ReadFile(rsa.PublicKeyPath)
if err != nil {
log.Error(err)
return err
}
publicKey, err := jwt.ParseRSAPublicKeyFromPEM(publicKeyData)
if err != nil {
log.Error("error public key: ", err)
return err
}
rsa.PublicKey = publicKey

// Get the private key
privateKeyData, err := ioutil.ReadFile(rsa.PrivateKeyPath)
if err != nil {
log.Error(err)
return err
}
privateKey, err := jwt.ParseRSAPrivateKeyFromPEMWithPassword(privateKeyData, goDotEnvVariable("RSA_PASSWORD"))
if err != nil {
log.Error("error public key: ", err)
return err
}
rsa.PrivateKey = privateKey

return nil
}

// GenerateToken creates a JWT with email and expiration time in the payload
func (auth Authenticator) GenerateToken(email string) (string, error) {
err := initRsaKeys()
if err != nil {
return "", errors.New("Couldn't init rsa keys")
}

validTime, _ := strconv.ParseInt(goDotEnvVariable("TOKEN_VALID_DURATION"), 10, 64)
// Generate Expiration date
expirationTime := time.Now().Add(time.Duration(validTime) * time.Minute)

claims := &Claim{
Email: email,
StandardClaims: jwt.StandardClaims{
// JWT takes unix timestamps
ExpiresAt: expirationTime.Unix(),
},
}

token := jwt.NewWithClaims(jwt.SigningMethodRS256, claims)
tokenString, err := token.SignedString(rsa.PrivateKey)

if err != nil {
log.Error("error while generating token: ", err)
return "", err
}

return tokenString, nil
}

// DecodeToken decode and validates a token
func (auth Authenticator) DecodeToken(tokenString string) (*jwt.Token, *Claim, error) {
err := initRsaKeys()
if err != nil {
return nil, &Claim{}, errors.New("Couldn't init rsa keys")
}

claims := &Claim{}

token, err := jwt.ParseWithClaims(tokenString, claims, func(token *jwt.Token) (interface{}, error) {
return rsa.PublicKey, nil
})
if err != nil {
log.Error("Couldn't parse the token : ", err)
return nil, &Claim{}, err
}

return token, claims, nil
}
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,18 @@ go 1.15

require (
github.com/AlecAivazis/survey/v2 v2.2.9
github.com/edwinvautier/go-boilerplate v0.0.0-20210322134926-eb821f8d79a6 // indirect
github.com/dgrijalva/jwt-go v3.2.0+incompatible
github.com/gobuffalo/packd v1.0.0
github.com/gobuffalo/packr/v2 v2.8.1
github.com/joho/godotenv v1.3.0
github.com/karrick/godirwalk v1.16.1 // indirect
github.com/mattn/go-colorable v0.1.8 // indirect
github.com/mgutz/ansi v0.0.0-20200706080929-d51e80ef957d // indirect
github.com/mitchellh/go-homedir v1.1.0
github.com/sirupsen/logrus v1.8.1
github.com/spf13/cobra v1.1.3
github.com/spf13/viper v1.7.1
github.com/stretchr/testify v1.7.0 // indirect
golang.org/x/crypto v0.0.0-20210322153248-0c34fe9e7dc2 // indirect
golang.org/x/sys v0.0.0-20210324051608-47abb6519492 // indirect
golang.org/x/term v0.0.0-20210317153231-de623e64d2a6 // indirect
Expand Down
Loading

0 comments on commit 4494c6a

Please sign in to comment.