Rename env vars to CLIENT_ID/CLIENT_SECRET and require at startup
This commit is contained in:
13
main.go
13
main.go
@@ -26,12 +26,13 @@ var schema string
|
|||||||
var templates *template.Template
|
var templates *template.Template
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
dsn := os.Getenv("PGCONN")
|
for _, key := range []string{"PGCONN", "CLIENT_ID", "CLIENT_SECRET"} {
|
||||||
if dsn == "" {
|
if os.Getenv(key) == "" {
|
||||||
log.Fatal("PGCONN environment variable is required")
|
log.Fatalf("%s environment variable is required", key)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
db, err := sql.Open("postgres", dsn)
|
db, err := sql.Open("postgres", os.Getenv("PGCONN"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatalf("failed to open database: %v", err)
|
log.Fatalf("failed to open database: %v", err)
|
||||||
}
|
}
|
||||||
@@ -114,7 +115,7 @@ func handleGoogleCallback(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
payload, err := idtoken.Validate(context.Background(), credential, os.Getenv("GOOGLE_CLIENT_ID"))
|
payload, err := idtoken.Validate(context.Background(), credential, os.Getenv("CLIENT_ID"))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Println("failed to validate token:", err)
|
log.Println("failed to validate token:", err)
|
||||||
http.Error(w, "invalid token", http.StatusUnauthorized)
|
http.Error(w, "invalid token", http.StatusUnauthorized)
|
||||||
@@ -135,7 +136,7 @@ func handleGoogleCallback(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func signEmail(email string) string {
|
func signEmail(email string) string {
|
||||||
h := hmac.New(sha256.New, []byte(os.Getenv("TOKEN_SECRET")))
|
h := hmac.New(sha256.New, []byte(os.Getenv("CLIENT_SECRET")))
|
||||||
h.Write([]byte(email))
|
h.Write([]byte(email))
|
||||||
sig := base64.RawURLEncoding.EncodeToString(h.Sum(nil))
|
sig := base64.RawURLEncoding.EncodeToString(h.Sum(nil))
|
||||||
return base64.RawURLEncoding.EncodeToString([]byte(email)) + "." + sig
|
return base64.RawURLEncoding.EncodeToString([]byte(email)) + "." + sig
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
const CLIENT_ID = '{{.env.GOOGLE_CLIENT_ID}}';
|
const CLIENT_ID = '{{.env.CLIENT_ID}}';
|
||||||
|
|
||||||
function getProfile() {
|
function getProfile() {
|
||||||
const data = localStorage.getItem('profile');
|
const data = localStorage.getItem('profile');
|
||||||
|
|||||||
Reference in New Issue
Block a user