Skip to content

Commit

Permalink
Update .gitignore and go.mod, delete unused files
Browse files Browse the repository at this point in the history
  • Loading branch information
devhindo committed Mar 19, 2024
1 parent d89c207 commit 7fdfbf4
Show file tree
Hide file tree
Showing 13 changed files with 198 additions and 33 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,7 @@
go.work

# local
bin
bin

# local
.env
64 changes: 64 additions & 0 deletions extenral/jwt/jwt.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package jwt

// implement register user

import (
"fmt"
"time"

"github.com/golang-jwt/jwt/v5"
)

type User struct {
Username string `json:"username"`
Password string `json:"password"`
Email string `json:"email"`
}

type Params struct {
key []byte
t *jwt.Token
s string
}



func createToken(u User, s []byte) (string, error) {
token := jwt.NewWithClaims(jwt.SigningMethodHS256,
jwt.MapClaims{
"username": u.Username,
"exp": time.Now().Add(time.Hour * 4).Unix(),
})

tokenString, err := token.SignedString(s)

if err != nil {
return "", err
}

return tokenString, nil
}

func VerifyToken(tokenString string, secret []byte) error {
token, err := jwt.Parse(tokenString, func(token *jwt.Token) (interface{}, error) {
return secret, nil
})

if err != nil {
return err
}

if !token.Valid {
return fmt.Errorf("invalid token")
}

return nil
}

func RegisterUser(u User, key []byte) {

// k := key
// t := jwt.New(jwt.SigningMethodHS256)
// s := t.SignedString(key)
//
}
27 changes: 0 additions & 27 deletions extenral/jwt/register.go

This file was deleted.

1 change: 1 addition & 0 deletions extenral/vault/README
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
https://github.com/hashicorp/vault
53 changes: 53 additions & 0 deletions extenral/vault/vault.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package vault

import (
"bytes"
"net/http"
"net/url"

"github.com/joho/godotenv"
)



func GetEnvVariable(name s) {
env := ""
return env
}


/*
HCP_API_TOKEN=$(curl --location "https://auth.idp.hashicorp.com/oauth2/token" \
--header "Content-Type: application/x-www-form-urlencoded" \
--data-urlencode "client_id=$HCP_CLIENT_ID" \
--data-urlencode "client_secret=$HCP_CLIENT_SECRET" \
--data-urlencode "grant_type=client_credentials" \
--data-urlencode "audience=https://api.hashicorp.cloud" | jq -r .access_token)
*/

func generate_HCP_API_TOKEN(HCP_CLIENT_ID string, HCP_CLIENT_SECRET string) (string, error) {
data := url.Values{}
data.Set("client_id", "HCP_CLIENT_ID")
data.Set("client_secret", HCP_CLIENT_SECRET)
data.Set("grant_type", "client_credentials")
data.Set("audience", "https://api.hashicorp.cloud")

req, err := http.NewRequest("POST", "https://auth.idp.hashicorp.com/oauth2/token", bytes.NewBufferString(data.Encode()))
if err != nil {
return "", err
}
a:=http.StatusAccepted

req.Header.Add("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set()


}

/*
-- read secrets
curl \
--location "env.SECRET_LOC"\
--request GET \
--header "Authorization: Bearer $HCP_API_TOKEN" | jq
*/
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ module github.com/devhindo/bats

go 1.21.1

require github.com/golang-jwt/jwt/v5 v5.2.1 // indirect
require (
github.com/golang-jwt/jwt/v5 v5.2.1 // indirect
github.com/joho/godotenv v1.5.1 // indirect
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
github.com/golang-jwt/jwt/v5 v5.2.1 h1:OuVbFODueb089Lh128TAcimifWaLhJwVflnrgM17wHk=
github.com/golang-jwt/jwt/v5 v5.2.1/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
22 changes: 22 additions & 0 deletions internal/auth/login.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package auth

import (
"encoding/json"
"net/http"

"github.com/devhindo/bats/types"

Check failure on line 7 in internal/auth/login.go

View workflow job for this annotation

GitHub Actions / build

no required module provides package github.com/devhindo/bats/types; to add it:
)

func LoginHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")

var u types.User

json.NewDecoder(r.Body).Decode(&u)

if u.Username == "admin" && u.Password == "admin" {
w.WriteHeader(http.StatusOK)
w.Write([]byte(`{"message": "success"}`))
return
}
}
2 changes: 1 addition & 1 deletion internal/auth/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ package auth

import (

"github.com/golang-jwt/jwt/v5"
// "github.com/golang-jwt/jwt/v5"
)
37 changes: 37 additions & 0 deletions internal/env/env.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package env

import (
"os"
"log"

"github.com/joho/godotenv"
)

var envVars map[string]string

func init() {
err := godotenv.Load()
if err != nil {
log.Fatal("Error loading .env file")
}

envVars = make(map[string]string)

keys := []string{"AHMED", "HCP_CLIENT_ID", /*... add more keys here ...*/}

for _, key := range keys {
if value, exists := os.LookupEnv(key); exists {
envVars[key] = value
} else {
log.Printf("Key \"%s\" not found", key)
}
}
}

func GetEnv(key string) string {
if value, exists := envVars[key]; exists {
return value
}
log.Printf("Key \"%s\" not found", key)
return ""
}
4 changes: 4 additions & 0 deletions internal/types/types.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
package types


type User struct {
Username string `json:"username"`
Email string `json:"email"`
}
8 changes: 6 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package main

import (
"github.com/devhindo/bats/internal/app"
// "github.com/devhindo/bats/internal/app"
"fmt"

"github.com/devhindo/bats/internal/env"
)

func main() {
app.RUN()
//app.RUN()
fmt.Println(env.GetEnv("AHMED"))
}
1 change: 0 additions & 1 deletion types/user.go

This file was deleted.

0 comments on commit 7fdfbf4

Please sign in to comment.