Skip to content

Commit

Permalink
Refactor: format files
Browse files Browse the repository at this point in the history
  • Loading branch information
cp-sumi-k committed Aug 3, 2023
1 parent f4cf01c commit 14a68a3
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 27 deletions.
8 changes: 3 additions & 5 deletions email/email.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ type EmailData struct {
// Attachment bytes if any
FileBytes *bytes.Buffer

// Attachment file name.
// Attachment file name.
// It should be with extension
FileName string

Expand Down Expand Up @@ -88,12 +88,12 @@ func getEmailTemplate(data *EmailData) (rawMail bool, emailTemplate interface{},
// Get Email with data
htmlBody, err := getEmailWithDataTemplate(data)

if err == nil{
if err == nil {
if rawMail {
emailTemplate = prepareRawEmailTemplate(htmlBody, data)
} else {
emailTemplate = prepareEmailTemplate(htmlBody, data)
}
}
}

return rawMail, emailTemplate, err
Expand Down Expand Up @@ -157,7 +157,6 @@ func prepareRawEmailTemplate(htmlBody string, data *EmailData) (template *ses.Se
var emailRaw bytes.Buffer
msg.WriteTo(&emailRaw)


// Message for raw email
message := ses.RawMessage{
Data: emailRaw.Bytes(),
Expand All @@ -173,4 +172,3 @@ func prepareRawEmailTemplate(htmlBody string, data *EmailData) (template *ses.Se

return template
}

19 changes: 9 additions & 10 deletions examples/jwtAuth.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,35 +12,34 @@ func GenerateTokens() {
refreshKey := "YOUR-JWT-REFRESH-KEY"

claims := map[string]interface{}{
"user_id" : 1,
"user_id": 1,
}

accessToken := jwtAuth.GenerateAccessToken(accessKey, claims, 24) // 1 day, lifespan in hour ,
accessToken := jwtAuth.GenerateAccessToken(accessKey, claims, 24) // 1 day, lifespan in hour ,

refreshToken := jwtAuth.GenerateRefreshToken(refreshKey, claims, 8760) // 1 year, lifespan in hour

fmt.Printf("Access token: %s ", &accessToken)
fmt.Printf("Refresh token: %s ", &refreshToken)
}

func checkValidationsOFTokens(accessToken string, refreshToken string){
func checkValidationsOFTokens(accessToken string, refreshToken string) {
accessKey := "YOUR-JWT-ACCESS-KEY"
refreshKey := "YOUR-JWT-REFRESH-KEY"

claims, err := jwtAuth.ValidateAccessTokenWithData(accessToken, accessKey)

if err != nil{
fmt.Errorf("Error when validating accessToken: ", err)
if err != nil {
fmt.Errorf("Error when validating accessToken: ", err)
}

fmt.Printf("Claims: %v", claims)

validRefreshToken, err := jwtAuth.ValidateRefreshToken(refreshToken, refreshKey)

if err != nil{
if err != nil {
fmt.Errorf("Error when validating refreshToken: ", err)
}

fmt.Printf("validRefreshToken: %b", validRefreshToken)
}
}

fmt.Printf("validRefreshToken: %b", validRefreshToken)
}
24 changes: 13 additions & 11 deletions jwtAuth/jwtAuth.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@ type JWTData struct {
CustomClaims map[string]interface{} `json:"custom,omitempty"`
}

/**
/**
Generate accessToken usin given data
accessKey: Secret access key
accessKey: Secret access key
customClaims: your custom claims you want to add in token like user_id, device_id etc...
lifeSpan: accessToken life time in hours
**/
Expand All @@ -28,22 +28,22 @@ func GenerateAccessToken(accessKey string, customClaims map[string]interface{},
return token, err
}

/**
/**
Generate refreshToken usin given data
refreshKey: Secret refresh key
refreshKey: Secret refresh key
customClaims: your custom claims you want to add in token like user_id, device_id etc...
lifeSpan: accessToken life time in hours
**/

func GenerateRefreshToken(refreshKey string, customClaims map[string]interface{}, lifeSpan int) (string, error) {

token, err := tokenWithClaims(customClaims, lifeSpan).SignedString([]byte(refreshKey))

return token, err
}

func tokenWithClaims(customClaims map[string]interface{}, lifeSpan int) *jwt.Token{
func tokenWithClaims(customClaims map[string]interface{}, lifeSpan int) *jwt.Token {
claims := JWTData{
StandardClaims: jwt.StandardClaims{
ExpiresAt: time.Now().Add(time.Duration(lifeSpan) * time.Hour).Unix(),
Expand All @@ -54,12 +54,14 @@ func tokenWithClaims(customClaims map[string]interface{}, lifeSpan int) *jwt.Tok
return jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
}

/**
/*
*
Validate given token using given key
token: Access Token you want to validate
key: Secret access key
**/
key: Secret access key
*
*/
func ValidateAccessTokenWithData(token string, key string) (map[string]interface{}, error) {

claims := &JWTData{}
Expand All @@ -86,11 +88,11 @@ func ValidateAccessTokenWithData(token string, key string) (map[string]interface
return claims.CustomClaims, err
}

/**
/**
Validate given refreshToken using given refreshKey
token: Refresh Token you want to validate
key: Secret refresh key
key: Secret refresh key
**/

func ValidateRefreshToken(token string, key string) (bool, error) {
Expand Down
1 change: 0 additions & 1 deletion jwtAuth/jwt_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package jwtAuth

import (

"testing"

"github.com/stretchr/testify/assert"
Expand Down

0 comments on commit 14a68a3

Please sign in to comment.