Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.env*
vote
.idea/
.idea/
.vscode/
4 changes: 2 additions & 2 deletions constitutional.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ func InitConstitution() {
startOfDay := time.Date(t.Year(), t.Month(), t.Day(), 0, 0, 0, 0, time.Local)
nextMidnight := startOfDay.AddDate(0, 0, 1)
fmt.Println(nextMidnight)
fmt.Println(nextMidnight.Sub(time.Now()))
ticker := time.NewTicker(nextMidnight.Sub(time.Now()))
fmt.Println(time.Until(nextMidnight))
ticker := time.NewTicker(time.Until(nextMidnight))
first := true
go func() {
for {
Expand Down
6 changes: 3 additions & 3 deletions database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const (
Updated UpsertResult = 1
)

var Client *mongo.Client = Connect()
var Client *mongo.Client
var db = ""

func Connect() *mongo.Client {
Expand All @@ -33,7 +33,7 @@ func Connect() *mongo.Client {

logging.Logger.WithFields(logrus.Fields{"module": "database", "method": "Connect"}).Info("beginning database connection")

ctx, cancel := context.WithTimeout(context.TODO(), 10*time.Second)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

uri := os.Getenv("VOTE_MONGODB_URI")
Expand All @@ -54,7 +54,7 @@ func Connect() *mongo.Client {
}

func Disconnect() {
ctx, cancel := context.WithTimeout(context.TODO(), 10*time.Second)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()

if err := Client.Disconnect(ctx); err != nil {
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go 1.24.1
require (
github.com/computersciencehouse/csh-auth v0.1.0
github.com/gin-gonic/gin v1.11.0
github.com/joho/godotenv v1.5.1
github.com/sirupsen/logrus v1.9.3
github.com/slack-go/slack v0.17.3
github.com/stretchr/testify v1.11.1
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg=
github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/klauspost/compress v1.18.1 h1:bcSGx7UbpBqMChDtsF28Lw6v/G94LPrrbMbdC3JH2co=
Expand Down
3 changes: 3 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/computersciencehouse/vote/logging"
"github.com/computersciencehouse/vote/sse"
"github.com/gin-gonic/gin"
"github.com/joho/godotenv"
"github.com/sirupsen/logrus"
"go.mongodb.org/mongo-driver/bson/primitive"
"mvdan.cc/xurls/v2"
Expand Down Expand Up @@ -57,6 +58,8 @@ func MakeLinks(s string) template.HTML {
var oidcClient = OIDCClient{}

func main() {
godotenv.Load()
database.Client = database.Connect()
r := gin.Default()
r.StaticFS("/static", http.Dir("static"))
r.SetFuncMap(template.FuncMap{
Expand Down
14 changes: 8 additions & 6 deletions users.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,22 +64,22 @@ func (client *OIDCClient) getAccessToken() int {
authData.Set("grant_type", "client_credentials")
resp, err := htclient.PostForm(cshAuth.ProviderURI+"/protocol/openid-connect/token", authData)
if err != nil {
logging.Logger.WithFields(logrus.Fields{"method": "setupOidcClient"}).Error(err)
logging.Logger.WithFields(logrus.Fields{"method": "getAccessToken"}).Error(err)
return 0
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
logging.Logger.WithFields(logrus.Fields{"method": "setupOidcClient", "statusCode": resp.StatusCode}).Error(resp.Status)
logging.Logger.WithFields(logrus.Fields{"method": "getAccessToken", "statusCode": resp.StatusCode}).Error(resp.Status)
return 0
}
respData := make(map[string]interface{})
err = json.NewDecoder(resp.Body).Decode(&respData)
if err != nil {
logging.Logger.WithFields(logrus.Fields{"method": "setupOidcClient"}).Error(err)
logging.Logger.WithFields(logrus.Fields{"method": "getAccessToken"}).Error(err)
return 0
}
if respData["error"] != nil {
logging.Logger.WithFields(logrus.Fields{"method": "setupOidcClient"}).Error(respData)
logging.Logger.WithFields(logrus.Fields{"method": "getAccessToken"}).Error(respData)
return 0
}
client.accessToken = respData["access_token"].(string)
Expand All @@ -91,19 +91,20 @@ func (client *OIDCClient) GetActiveUsers() []OIDCUser {
//active
req, err := http.NewRequest("GET", client.providerBase+"/auth/admin/realms/csh/groups/a97a191e-5668-43f5-bc0c-6eefc2b958a7/members", nil)
if err != nil {
logging.Logger.WithFields(logrus.Fields{"method": "GetActiveUsers"}).Error(err)
return nil
}
req.Header.Add("Authorization", "Bearer "+client.accessToken)
resp, err := htclient.Do(req)
if err != nil {
logging.Logger.WithFields(logrus.Fields{"method": "GetAllUsers"}).Error(err)
logging.Logger.WithFields(logrus.Fields{"method": "GetActiveUsers"}).Error(err)
return nil
}
defer resp.Body.Close()
ret := make([]OIDCUser, 0)
err = json.NewDecoder(resp.Body).Decode(&ret)
if err != nil {
logging.Logger.WithFields(logrus.Fields{"method": "GetAllUsers"}).Error(err)
logging.Logger.WithFields(logrus.Fields{"method": "GetActiveUsers"}).Error(err)
return nil
}
return ret
Expand All @@ -118,6 +119,7 @@ func (client *OIDCClient) GetUserInfo(user *OIDCUser) {
req, err := http.NewRequest("GET", client.providerBase+"/auth/admin/realms/csh/users/"+user.Uuid+arg, nil)
// also "users/{user-id}/groups"
if err != nil {
logging.Logger.WithFields(logrus.Fields{"method": "GetUserInfo"}).Error(err)
return
}
req.Header.Add("Authorization", "Bearer "+client.accessToken)
Expand Down