Broken mess, got to fix.
Some checks failed
ci/woodpecker/push/woodpecker Pipeline failed

This commit is contained in:
Midou36O 2022-09-16 23:10:37 +01:00
parent 5244119ffe
commit 31556d3a6b
Signed by: midou
GPG Key ID: 08063D5407090BC2
6 changed files with 86 additions and 78 deletions

View File

@ -1,56 +1,15 @@
package oauth
package api
import (
"errors"
"io"
"io/ioutil"
"log"
"net/http"
"os"
"time"
"x/oauth2"
"github.com/ProjectSegfault/segfautils/config"
"github.com/goccy/go-json"
)
var (
clientID = config.OAuthClientID()
clientSecret = config.OAuthClientSecret()
redirectURL = config.RedirectURL()
authURL = config.AuthURL()
redirectURL = config.OAuthRedirectURL()
authURL = config.OAuthURL()
)
func LoginOAuth() {
// Create a new redirect route route
http.HandleFunc("/oauth/redirect", func(w http.ResponseWriter, r *http.Request) {
err := r.ParseForm()
if err != nil {
fmt.Fprintf(os.Stdout, "could not parse query: %v", err)
w.WriteHeader(http.StatusBadRequest)
}
code := r.FormValue("code")
reqURL := fmt.Sprintf("%s/login/oauth/access_token?client_id=%s&client_secret=%s&code=%s", authURL, clientID, clientSecret, code)
req, err := http.NewRequest(http.MethodPost, reqURL, nil)
if err != nil {
fmt.Fprintf(os.Stdout, "could not create HTTP request: %v", err)
w.WriteHeader(http.StatusBadRequest)
}
req.Header.Set("accept", "application/json")
res, err := httpClient.Do(req)
if err != nil {
fmt.Fprintf(os.Stdout, "could not send HTTP request: %v", err)
w.WriteHeader(http.StatusInternalServerError)
}
defer res.Body.Close()
var t OAuthAccessResponse
if err := json.NewDecoder(res.Body).Decode(&t); err != nil {
fmt.Fprintf(os.Stdout, "could not parse JSON response: %v", err)
w.WriteHeader(http.StatusBadRequest)
}
w.Header().Set("Location", "/announcements/?access_token="+t.AccessToken)
w.WriteHeader(http.StatusFound)
}
type OAuthAccessResponse struct {
AccessToken string `json:"access_token"`
}
}

View File

@ -6,7 +6,7 @@ import (
"github.com/spf13/viper"
)
func RedirectURL() string {
func OAuthRedirectURL() string {
viper.SetConfigName("config")
viper.AddConfigPath("./data")
err := viper.ReadInConfig()

View File

@ -6,7 +6,7 @@ import (
"github.com/spf13/viper"
)
func AuthURL() string {
func OAuthURL() string {
viper.SetConfigName("config")
viper.AddConfigPath("./data")
err := viper.ReadInConfig()

8
data/lol.toml Normal file
View File

@ -0,0 +1,8 @@
[segfautils]
port = 6893
webhook_url = "https://127.0.0.1:8080/segfautils"
auth_token = "121212"
[hcaptcha]
site_key = "lol"
secret_key = "lol2"

View File

@ -2,7 +2,7 @@
Web utilities for Project Segfault
## What does it do?
For now it powers our contact form. In the future we will expand our APIs so you can do more cool things.
For now it powers our contact form and our announcements page. In the future we will expand our APIs so you can do more cool things. (We currently do not have any new idea so please open issues and give us suggestions!!)
## Setup
@ -20,8 +20,8 @@ If you're using Portainer, you should know how to add Segfautils.
```
git clone https://github.com/ProjectSegfault/segfautils
cd segfautils/
# You need to add the environment HCAPTCHA_SITE_KEY, HCAPTCHA_SECRET_KEY, SEGFAUTILS_WEBHOOK_URL and SEGFAUTILS_PORT.
# You need to config file located in data/config.toml.
go run main.go # Run this when you've done above, and you're planning on developing, if not, do below
go build . -o segfautils
./segfautils
```
```

View File

@ -2,37 +2,78 @@
package utils
import (
"log"
"fmt"
"os"
)
"github.com/ProjectSegfault/segfautils/config"
var (
is = 0
arrName = [9]string{"Port", "Authentication token", "Webhook URL", "HCaptcha secret", "Hcaptcha site key", "OAuth client ID", "OAuth client secret", "OAuth redirect URL", "OAuth athentication URL"}
arrCfg = [9]string{"0", "YOURAUTHTOKEN", "YOURWEBHOOKURL", "YOURSECRETKEY", "YOURSITEKEY", "YOURCLIENTID", "YOURCLIENTSECRET", "YOURREDIRECTURL", "YOURAUTHURL"}
testCfg = [9]string{"config.Authtoken()", "config.WebhookURL()", "config.HCaptchaSecretKey()", "config.HCaptchaSiteKey()", "config.OAuthClientID()", "config.OAuthClientSecret()", "config.OAuthRedirectURL()", "config.OAuthURL()"}
)
func CheckConfig() {
if config.Port() == "0" {
log.Fatal("[Segfautils] ❌ You need to set the port you'd like to use in the config file. Check documentation for more information.")
} else {
log.Println("[Segfautils] ✅ segfautils.port is set to", config.Port())
for i := 0; i < 8; i++ {
if testCfg[i] == arrCfg[i] || testCfg[i] == "" {
fmt.Println(arrCfg[i])
is = i
Fail()
} else {
fmt.Println(arrCfg[i])
fmt.Println(testCfg[i])
is = i
Check()
}
}
if config.AuthToken() == "YOURAUTHTOKEN" || config.AuthToken() == "" {
log.Fatal("[Segfautils] ❌ You need to set the authentication token you'd like to use in the config file. Check documentation for more information.")
} else {
log.Println("[Segfautils] ✅ segfautils.auth_token is set!")
}
if config.WebhookURL() == "YOURWEBHOOKURL" || config.WebhookURL() == "" {
log.Fatal("[Segfautils] ❌ You need to set the Webhook URL you'd like to use in the config file. Check documentation for more information.")
} else {
log.Println("[Segfautils] ✅ segfautils.webhook_url is set!")
}
// Hcaptcha stuff
if config.HCaptchaSecretKey() == "YOURSECRETKEY" || config.HCaptchaSecretKey() == "" {
log.Fatal("[Segfautils] ❌ You need to set the HCaptcha secret you'd like to use in the config file. Check documentation for more information.")
} else {
log.Println("[Segfautils] ✅ segfautils.hcaptcha_secret is set!")
}
if config.HCaptchaSiteKey() == "YOURSITEKEY" || config.HCaptchaSiteKey() == "" {
log.Println("[Segfautils] ⚠️ The HCaptcha site key isn't set. You don't have to, but the demo form will not work without it. Check documentation for more information.")
} else {
log.Println("[Segfautils] ✅ hcaptcha.site_key is set!")
}
log.Println("[Segfautils] ✅ All config checks passed!")
}
/*
func CheckConfig() {
if config.Port() == "0" {
Fail()
} else {
Check()
}
if config.AuthToken() == "YOURAUTHTOKEN" || config.AuthToken() == "" {
log.Fatal("[Segfautils] ❌ You need to set the authentication token you'd like to use in the config file. Check documentation for more information.")
} else {
log.Println("[Segfautils] ✅ segfautils.auth_token is set!")
}
if config.WebhookURL() == "YOURWEBHOOKURL" || config.WebhookURL() == "" {
log.Fatal("[Segfautils] ❌ You need to set the Webhook URL you'd like to use in the config file. Check documentation for more information.")
} else {
log.Println("[Segfautils] ✅ segfautils.webhook_url is set!")
}
// Hcaptcha stuff
if config.HCaptchaSecretKey() == "YOURSECRETKEY" || config.HCaptchaSecretKey() == "" {
log.Fatal("[Segfautils] ❌ You need to set the HCaptcha secret you'd like to use in the config file. Check documentation for more information.")
} else {
log.Println("[Segfautils] ✅ segfautils.hcaptcha_secret is set!")
}
if config.HCaptchaSiteKey() == "YOURSITEKEY" || config.HCaptchaSiteKey() == "" {
log.Println("[Segfautils] ⚠️ The HCaptcha site key isn't set. You don't have to, but the demo form will not work without it. Check documentation for more information.")
} else {
log.Println("[Segfautils] ✅ hcaptcha.site_key is set!")
}
log.Println("[segfautils] ✅ All config checks passed!")
log.Println("[segfautils] ✅ Listening at port ", config.Port())
}
*/
func Check() {
fmt.Printf("[Segfautils] ✅ %s is set!\n", arrName[is])
}
func Fail() {
fmt.Printf("[Segfautils] ❌ You need to set the %s you'd like to use in the config file. Check documentation for more information.\n", arrName[is])
os.Exit(Error())
}
func OptionalFail() {
fmt.Printf("[Segfautils] ⚠️ The %s isn't set. You don't have to, but the program will not be able to behave correctly. Check documentation for more information.\n", arrName[is])
}
func Error() int {
return 12
}