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 comments for better understanding
  • Loading branch information
Bikram-ghuku committed Jun 13, 2024
commit da917a4921446bd7d8622368a2b2c09fb8001f3b
15 changes: 13 additions & 2 deletions backend/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,8 @@ func GhAuth(w http.ResponseWriter, r *http.Request) {
gh_pubKey := os.Getenv("GH_CLIENT_ID")
gh_pvtKey := os.Getenv("GH_PRIVATE_ID")
jwt_key := os.Getenv("TOKEN")

// Get the access token for authenticating other endpoints
uri := fmt.Sprintf("https://github.com/login/oauth/access_token?client_id=%s&client_secret=%s&code=%s", gh_pubKey, gh_pvtKey, bodyReg.GhCode)

req, _ := http.NewRequest("POST", uri, nil)
Expand All @@ -354,12 +356,15 @@ func GhAuth(w http.ResponseWriter, r *http.Request) {
return
}
defer resp.Body.Close()

// Decode the response
var tokenResponse GithubAccessTokenResponse
if err := json.NewDecoder(resp.Body).Decode(&tokenResponse); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

// Get the username of the user who made the request
req, _ = http.NewRequest("GET", "https://api.github.com/user", nil)
req.Header.Set("Authorization", "Bearer "+tokenResponse.AccessToken)

Expand All @@ -370,22 +375,25 @@ func GhAuth(w http.ResponseWriter, r *http.Request) {
}
defer resp.Body.Close()

// Decode the response
var userResponse GithubUserResponse
if err := json.NewDecoder(resp.Body).Decode(&userResponse); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}

uname := userResponse.Login

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

// Get check parameters
org_name := os.Getenv("ORG_NAME")
org_team := os.Getenv("ORG_TEAM_SLUG")

// Send request to check status of the user in the given org's 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)
Expand All @@ -401,17 +409,19 @@ func GhAuth(w http.ResponseWriter, r *http.Request) {
}

defer resp.Body.Close()

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

// Check if user is present in the team
if checkResp.State != "active" {
http.Error(w, "User is not authenticated", http.StatusUnauthorized)
return
}

// Create the response JWT
token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{
"name": uname,
harshkhandeparkar marked this conversation as resolved.
Show resolved Hide resolved
})
Expand All @@ -424,6 +434,7 @@ func GhAuth(w http.ResponseWriter, r *http.Request) {

http.Header.Add(w.Header(), "content-type", "application/json")

// Send the response
err = json.NewEncoder(w).Encode(&tokenString)
harshkhandeparkar marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
Expand Down