-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update .gitignore and go.mod, delete unused files
- Loading branch information
Showing
13 changed files
with
198 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,4 +21,7 @@ | |
go.work | ||
|
||
# local | ||
bin | ||
bin | ||
|
||
# local | ||
.env |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
// | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
https://github.com/hashicorp/vault |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
) | ||
|
||
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,5 +3,5 @@ package auth | |
|
||
import ( | ||
|
||
"github.com/golang-jwt/jwt/v5" | ||
// "github.com/golang-jwt/jwt/v5" | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 "" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")) | ||
} |
This file was deleted.
Oops, something went wrong.