Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added github OAuth endpoints #72

Merged
merged 34 commits into from
Jul 21, 2024
Merged
Changes from 1 commit
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
3dd7b53
feat: added jwt packages
Bikram-ghuku Jun 13, 2024
38c5389
feat: added basic github OAuth
Bikram-ghuku Jun 13, 2024
9578023
feat: added new env variables
Bikram-ghuku Jun 13, 2024
6d3ceaa
feat: added checks for github org team member
Bikram-ghuku Jun 13, 2024
52d671b
feat: removed slug conversion
Bikram-ghuku Jun 13, 2024
2441517
feat: updated template env to include org name and team name
Bikram-ghuku Jun 13, 2024
da917a4
feat: added comments for better understanding
Bikram-ghuku Jun 13, 2024
c8b458f
chore: added comments for better understanding
Bikram-ghuku Jun 13, 2024
bb153cd
feat: added JWT MiddleWare
Bikram-ghuku Jun 14, 2024
ed3ac4d
feat: added demo protected route
Bikram-ghuku Jun 14, 2024
c8f1028
fix: token in jwt parser
Bikram-ghuku Jun 14, 2024
a041e0e
chore: suggested changes
Bikram-ghuku Jun 14, 2024
c57baf5
feat: check if username is not nil
Bikram-ghuku Jun 14, 2024
0580b8b
feat: sending response as json object
Bikram-ghuku Jun 15, 2024
94729e5
Update backend/main.go
Bikram-ghuku Jun 15, 2024
66fedae
chore: commented the protected route for future reference
Bikram-ghuku Jun 15, 2024
cb7e257
Update backend/.env.template
Bikram-ghuku Jun 15, 2024
2424a1a
feat: suggested changes
Bikram-ghuku Jun 15, 2024
fbe5270
feat: comment protected route
Bikram-ghuku Jun 15, 2024
e047b83
feat: change from reading env to using env variable
Bikram-ghuku Jun 15, 2024
f875bc4
chore: update env var from TOKEN to JWT_TOKEN
Bikram-ghuku Jun 15, 2024
80c3960
feat: recommended changes
Bikram-ghuku Jun 15, 2024
9833a55
feat: removed unwanted comment
Bikram-ghuku Jun 15, 2024
fa29207
feat: fix variable name, update error message on empty ghcode
Bikram-ghuku Jun 15, 2024
e4b403b
feat: added checks for environment variables
Bikram-ghuku Jun 15, 2024
5398790
feat: added function for loading env
Bikram-ghuku Jun 24, 2024
22ba9fd
chore(vars): rename vars
Bikram-ghuku Jun 24, 2024
69fb3db
feat: update to go 1.22.4
Bikram-ghuku Jun 26, 2024
ba1d41a
refactor: 1.22 http handle func
Bikram-ghuku Jun 26, 2024
8a89139
feat: updated docker compose file
Bikram-ghuku Jun 26, 2024
77a77e9
feat: better error handling
Bikram-ghuku Jul 1, 2024
78d32b1
feat: update go version
Bikram-ghuku Jul 21, 2024
2296b0f
feat: fix merge conflicts
Bikram-ghuku Jul 21, 2024
d5532e9
fix: merge errors
Bikram-ghuku Jul 21, 2024
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
Prev Previous commit
Next Next commit
feat: added checks for github org team member
  • Loading branch information
Bikram-ghuku committed Jun 13, 2024
commit 6d3ceaafb293af644b7f4f6ad6792c2bc9cdafbd
50 changes: 46 additions & 4 deletions backend/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"net/http"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -308,6 +309,15 @@ func populateDB(filename string) error {
return nil
}

func ConvertTeamNameToSlug(teamName string) string {
slug := strings.ToLower(teamName)
slug = strings.ReplaceAll(slug, " ", "-")
re := regexp.MustCompile(`[^a-z0-9-]`)
slug = re.ReplaceAllString(slug, "")

return slug
}

func GhAuth(w http.ResponseWriter, r *http.Request) {
Bikram-ghuku marked this conversation as resolved.
Show resolved Hide resolved
if r.Method != "POST" {
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
Expand All @@ -325,10 +335,8 @@ func GhAuth(w http.ResponseWriter, r *http.Request) {
}

type GithubUserResponse struct {
Name string `json:"name"`
Login string `json:"login"`
ID int `json:"id"`
AvatarURL string `json:"avatar_url"`
Login string `json:"login"`
ID int `json:"id"`
}

bodyReg := BodyReg{}
Bikram-ghuku marked this conversation as resolved.
Show resolved Hide resolved
Expand Down Expand Up @@ -380,6 +388,40 @@ func GhAuth(w http.ResponseWriter, r *http.Request) {

uname := userResponse.Login

if uname == "" {
http.Error(w, "No user found", http.StatusUnauthorized)
return
}

org_name := os.Getenv("ORG_NAME")
org_team := ConvertTeamNameToSlug(os.Getenv("ORG_TEAM"))

url := fmt.Sprintf("https://api.github.com/orgs/%s/teams/%s/memberships/%s", org_name, org_team, uname)
req, _ = http.NewRequest("GET", url, nil)
req.Header.Set("Authorization", "Bearer "+tokenResponse.AccessToken)
resp, err = client.Do(req)

var checkResp struct {
State string `json:"state"`
}

if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

defer resp.Body.Close()

if err := json.NewDecoder(resp.Body).Decode(&checkResp); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

if checkResp.State != "active" {
http.Error(w, "User is not authenticated", http.StatusUnauthorized)
return
}

token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
"name": uname,
harshkhandeparkar marked this conversation as resolved.
Show resolved Hide resolved
})
Expand Down