Skip to content

Commit

Permalink
Made invite code and group optional. created build script
Browse files Browse the repository at this point in the history
  • Loading branch information
FrostWalk committed Sep 29, 2024
1 parent 02aceb9 commit 89065bb
Show file tree
Hide file tree
Showing 14 changed files with 271 additions and 57 deletions.
4 changes: 3 additions & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ Dockerfile
docker-compose.yml
LICENSE
README.md
assets/
assets/
build/
build.sh
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
gopath/
.idea/
.idea/
build/
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ CMD ["/inviter"]
ENV GITHUB_ORG_NAME=""
ENV GITHUB_TOKEN=""
ENV GITHUB_GROUP_NAME=""
ENV INVITE_CODE=""
ENV INVITE_CODE_HASH=""
ENV TLS_CERT=""
ENV TLS_KEY=""
ENV HTTP_PORT="80"
Expand Down
27 changes: 19 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# GitHub Inviter

GitHub Inviter is a web application that allows users to join a specific GitHub organization and team using an
invitation code. The application provides a simple web interface where users can enter their GitHub username and the
provided invitation code to get added to the organization's team.
GitHub Inviter is a web application that allows users to join a specific GitHub organization and a team (optional) using
an invitation code (optional). The application provides a simple web interface where users can enter their
GitHub username and the provided invitation code to get added to the organization's team.

## Features

Expand All @@ -14,6 +14,7 @@ provided invitation code to get added to the organization's team.
## Screenshot

![Screenshot](/assets/images/index.png)
![Screenshot](/assets/images/success.png)

## Configuration

Expand All @@ -23,13 +24,21 @@ The application is configured using environment variables. Here are the availabl
|----------------------|---------------------------------------------------------|----------|---------|
| `GITHUB_ORG_NAME` | The name of your GitHub organization | Yes | - |
| `GITHUB_TOKEN` | GitHub personal access token with necessary permissions | Yes | - |
| `GITHUB_GROUP_NAME` | The name of the team in your organization | Yes | - |
| `INVITE_CODE` | The invitation code users need to provide | Yes | - |
| `GITHUB_GROUP_NAME` | The name of the team in your organization | No | - |
| `INVITE_CODE_HASH` | The invitation code users need to provide | No | - |
| `HTTP_PORT` | The port on which the application will run | No | 80 |
| `HTTPS_PORT` | The port on which the application will run (https) | No | 443 |
| `TLS_CERT` | Path to the TLS certificate file | No | - |
| `TLS_KEY` | Path to the TLS key file | No | - |

### How to generate the INVITE_CODE_HASH

- On Linux:
- open a terminal
- write `echo -n 'invite code' | sha256sum` where *invite code* is your string **leave the '**
- On any other:
- go [here](https://emn178.github.io/online-tools/sha256.html)

## Running with Docker Compose

Here's an example `docker-compose.yml` file to run the GitHub Inviter:
Expand All @@ -43,10 +52,10 @@ services:
- GITHUB_ORG_NAME=your-org-name
- GITHUB_TOKEN=your-github-token
- GITHUB_GROUP_NAME=your-team-name
- INVITE_CODE=your-invite-code
- INVITE_CODE_HASH=your-invite-code
- HTTP_PORT=80
- HTTPS_PORT=443
# Uncomment the following lines if you want to use TLS
# - HTTPS_PORT=443
# - TLS_CERT=/path/to/your/cert.pem
# - TLS_KEY=/path/to/your/key.pem
ports:
Expand All @@ -68,7 +77,9 @@ To run the application:
docker-compose up -d
```

The application will be available at `http://127.0.0.1:8080` (or `https://127.0.0.1:8080` if TLS is configured).
The application will be available at `http://127.0.0.1/` (or `https://127.0.0.1/` if TLS is configured) unless
otherwise
specified with environment variables.

## About the token

Expand Down
Binary file added assets/images/success.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
44 changes: 44 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/bin/bash

# Nome del file Go che desideri compilare
SOURCE_FILE="main.go"

# Nome del binario risultante
OUTPUT_NAME="inviter"
OUTPUT_PATH="./build"

# Funzione per compilare l'applicazione
build() {
local os=$1
local arch=$2
local output=$3

echo "Compilazione per $os/$arch..."

# Impostiamo le variabili di ambiente per la compilazione
GOOS=$os GOARCH=$arch CGO_ENABLED=0 go build -ldflags="-s -w" -o "$OUTPUT_PATH/$output" $SOURCE_FILE

if [ $? -ne 0 ]; then
echo "Errore durante la compilazione per $os/$arch"
exit 1
fi

echo "Compilazione completata: $output"
}

# Compilazione per Windows (amd64)
build "windows" "amd64" "${OUTPUT_NAME}_windows_amd64.exe"

# Compilazione per Linux (amd64)
build "linux" "amd64" "${OUTPUT_NAME}_linux_amd64"

# Compilazione per Linux (arm64)
build "linux" "arm64" "${OUTPUT_NAME}_linux_arm64"

# Compilazione per macOS (Intel)
build "darwin" "amd64" "${OUTPUT_NAME}_macos_amd64"

# Compilazione per macOS (ARM - serie M)
build "darwin" "arm64" "${OUTPUT_NAME}_macos_arm64"

echo "Tutte le compilazioni sono completate."
44 changes: 31 additions & 13 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import (
type AppConfig struct {
OrgName string //mandatory
Token string //mandatory
GroupName string //mandatory
InviteCode []byte //mandatory
GroupName string //optional
InviteCode []byte //optional
HttpPort string //optional (default 80)
HttpsPort string //optional (default 443)
TlsCert string //optional
Expand All @@ -21,7 +21,15 @@ type AppConfig struct {

var conf AppConfig

func Load() bool {
type EnabledFeatures struct {
Code bool
Tls bool
Group bool
}

var features EnabledFeatures

func Load() {
// Check for mandatory environment variables
orgName := strings.Trim(os.Getenv("GITHUB_ORG_NAME"), " ")
if len(orgName) == 0 {
Expand All @@ -33,14 +41,14 @@ func Load() bool {
log.Fatal("GITHUB_TOKEN environment variable must be set")
}

inviteCode := strings.Trim(os.Getenv("INVITE_CODE"), " ")
if len(inviteCode) == 0 {
log.Fatal("INVITE_CODE environment variable must be set")
groupName := strings.Trim(os.Getenv("GITHUB_GROUP_NAME"), " ")
if len(groupName) != 0 {
features.Group = true
}

groupName := strings.Trim(os.Getenv("GITHUB_GROUP_NAME"), " ")
if len(groupName) == 0 {
log.Fatal("GROUP_NAME environment variable must be set")
inviteCode := strings.Trim(os.Getenv("INVITE_CODE_HASH"), " ")
if len(inviteCode) != 0 {
features.Code = true
}

// Set the optional environment variables, using defaults if not set
Expand All @@ -57,7 +65,7 @@ func Load() bool {
OrgName: orgName,
Token: token,
GroupName: strings.ToLower(groupName),
InviteCode: hash.CalculateHash(inviteCode),
InviteCode: hash.HexToByteArray(inviteCode),
HttpPort: httpPort,
HttpsPort: httpsPort,
TlsCert: strings.Trim(os.Getenv("TLS_CERT"), " "),
Expand All @@ -72,10 +80,8 @@ func Load() bool {
log.Fatalf("Key file: %s does not exist", conf.TlsKey)
}

return true
features.Tls = true
}

return false
}

func OrgName() string {
Expand Down Expand Up @@ -109,3 +115,15 @@ func TlsCert() string {
func TlsKey() string {
return conf.TlsKey
}

func IsTlsEnable() bool {
return features.Tls
}

func IsCodeRequired() bool {
return features.Code
}

func IsGroupEnable() bool {
return features.Group
}
2 changes: 1 addition & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ services:
- GITHUB_ORG_NAME=your-org-name
- GITHUB_TOKEN=your-github-token
- GITHUB_GROUP_NAME=your-team-name
- INVITE_CODE=your-invite-code
- INVITE_CODE_HASH=your-invite-code
- HTTP_PORT=80
- HTTPS_PORT=443
# Uncomment the following lines if you want to use TLS
Expand Down
31 changes: 31 additions & 0 deletions github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,37 @@ func AddUserToGroup(username string) error {
return nil
}

func AddUserToOrg(username string) error {
url := fmt.Sprintf("%s/orgs/%s/memberships/%s", baseUrl, config.OrgName(), username)
req, err := http.NewRequest("PUT", url, bytes.NewBuffer([]byte(`{"role":"member"}`)))
if err != nil {
return err
}

req.Header.Set("Authorization", "Bearer "+config.Token())
req.Header.Set("Accept", "application/vnd.github+json")
req.Header.Set("Content-Type", "application/json")

client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
return err
}

if resp.StatusCode == http.StatusUnauthorized {
return fmt.Errorf("the token does not have the required permissions")
}

defer resp.Body.Close()

if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated {
body, _ := io.ReadAll(resp.Body)
return fmt.Errorf("failed to add user to group: %s", string(body))
}

return nil
}

func GetOrgLogoUrl(orgName string) (string, error) {
url := fmt.Sprintf("%s/orgs/%s", baseUrl, orgName)
req, err := http.NewRequest("GET", url, nil)
Expand Down
53 changes: 38 additions & 15 deletions handlers/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,14 @@ var (
func InitCache() error {
cacheMutex.RLock()
var err error
templateCache, err = template.ParseFiles("./templates/index.html")

// choose the template based on whether the invitation code is needed
if config.IsCodeRequired() {
templateCache, err = template.ParseFiles("./templates/index-code.html")
} else {
templateCache, err = template.ParseFiles("./templates/index.html")
}

if err != nil {
cacheMutex.RUnlock()
return err
Expand All @@ -43,12 +50,17 @@ func MainPage(w http.ResponseWriter, _ *http.Request) {
cachedLogo := logoCache
cacheMutex.RUnlock()

name := config.OrgName()
if config.IsGroupEnable() {
name += "'s " // display org's team
}

data := struct {
OrgName string
LogoURL string
TeamName string
}{
OrgName: config.OrgName(),
OrgName: name,
LogoURL: cachedLogo,
TeamName: config.GroupName(),
}
Expand All @@ -70,21 +82,32 @@ func Submit(w http.ResponseWriter, r *http.Request) {
http.Error(w, "Username is required", http.StatusBadRequest)
return
}
inviteCode := strings.Trim(r.FormValue("inviteCode"), " ")
if inviteCode == "" {
http.Error(w, "Invite code is required", http.StatusBadRequest)
return
}
if !hash.Compare(inviteCode, config.InviteCode()) {
http.Error(w, "Invalid username or invitation code", http.StatusUnauthorized)
log.Printf("User: %s, tried to access with code: %s", username, inviteCode)
return

if config.IsCodeRequired() {
inviteCode := strings.Trim(r.FormValue("inviteCode"), " ")
if inviteCode == "" {
http.Error(w, "Invite code is required", http.StatusBadRequest)
return
}
if !hash.Compare(inviteCode, config.InviteCode()) {
http.Error(w, "Invalid username or invitation code", http.StatusUnauthorized)
log.Printf("User: %s, tried to access with code: %s", username, inviteCode)
return
}
}

err := github.AddUserToGroup(username)
if err != nil {
http.Error(w, fmt.Sprintf("Failed to add user to group: %v", err), http.StatusInternalServerError)
return
if config.IsGroupEnable() {
err := github.AddUserToGroup(username)
if err != nil {
http.Error(w, fmt.Sprintf("Failed to add user to group: %v", err), http.StatusInternalServerError)
return
}
} else {
err := github.AddUserToOrg(username)
if err != nil {
http.Error(w, fmt.Sprintf("Failed to add user to org: %v", err), http.StatusInternalServerError)
return
}
}

http.Redirect(w, r, "/success", http.StatusSeeOther)
Expand Down
28 changes: 22 additions & 6 deletions hash/hash.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,30 @@ package hash
import (
"bytes"
"crypto/sha256"
"encoding/hex"
"log"
)

func CalculateHash(password string) []byte {
h := sha256.New()
h.Write([]byte(password))
return h.Sum(nil)
func CalculateHash(code string) []byte {
hash := sha256.Sum256([]byte(code))
return hash[:]
}

func Compare(password string, hash []byte) bool {
return bytes.Equal(hash, CalculateHash(password))
func Compare(code string, hash []byte) bool {
return bytes.Equal(hash, CalculateHash(code))
}

func HexToByteArray(hexString string) []byte {
// Remove "0x" prefix if present
if len(hexString) >= 2 && hexString[:2] == "0x" {
hexString = hexString[2:]
}

// Decode hex string to byte slice
byteArray, err := hex.DecodeString(hexString)
if err != nil {
log.Fatalf("failed to decode hex string: %v", err)
}

return byteArray
}
Loading

0 comments on commit 89065bb

Please sign in to comment.