Skip to content

Commit

Permalink
fix: admin check
Browse files Browse the repository at this point in the history
  • Loading branch information
giammirove committed Feb 24, 2024
1 parent 7690559 commit d0ea9d2
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 15 deletions.
13 changes: 4 additions & 9 deletions auth/authenticator.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ import (
)

const (
SCOPES = "read:user,user:email"
SCOPES = "read:user,user:email,read:org,read:members"
)

var (
GithubAuthorizeURL, _ = url.Parse("https://github.com/login/oauth/authorize")
GithubAccessTokenURL, _ = url.Parse("https://github.com/login/oauth/access_token")
GithubUserURL, _ = url.Parse("https://api.github.com/user")
GithubMemberURL, _ = url.Parse("https://api.github.com/orgs/csunibo/members")
GithubMemberURL, _ = url.Parse("https://api.github.com/orgs/csunibo/memberships/")
client = http.DefaultClient
)

Expand Down Expand Up @@ -121,13 +121,8 @@ type GithubUserResponse struct {
Url string `json:"url"`
}
type GithubMemberUserResponse struct {
Id int `json:"id"`
Name string `json:"name"`
AvatarUrl string `json:"avatar_url"`
Email string `json:"email"`
Login string `json:"login"`
Url string `json:"url"`
IsAdmin bool `json:"site_admin"`
Message string `json:"message,omitempty"`
Role string `json:"role,omitempty"`
}

func (a *Authenticator) getUser(token string, res http.ResponseWriter, req *http.Request) (*User, error) {
Expand Down
11 changes: 5 additions & 6 deletions auth/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ func (a *Authenticator) CallbackHandler(res http.ResponseWriter, req *http.Reque
"token": token,
"user": user,
}
fmt.Println(token)

tokenString, err := jwt.NewWithClaims(jwt.SigningMethodHS256, claims).SignedString(a.signingKey)
if err != nil {
Expand Down Expand Up @@ -117,7 +118,7 @@ func (a *Authenticator) LoginHandler(res http.ResponseWriter, req *http.Request)
}

func (a *Authenticator) CheckMembership(token string, login string) (bool, error) {
reqHttp, err := http.NewRequest(http.MethodGet, GithubMemberURL.String(), nil)
reqHttp, err := http.NewRequest(http.MethodGet, fmt.Sprintf("%s/%s", GithubMemberURL.String(), login), nil)
if err != nil {
return false, fmt.Errorf("could not construct GitHub's user request: %w", err)
}
Expand All @@ -127,7 +128,7 @@ func (a *Authenticator) CheckMembership(token string, login string) (bool, error
if err != nil {
return false, fmt.Errorf("could not send GitHub's user request: %w", err)
}
var githubRes []GithubMemberUserResponse
var githubRes GithubMemberUserResponse
err = json.NewDecoder(resHttp.Body).Decode(&githubRes)
if err != nil {
return false, fmt.Errorf("could not parse GitHub's response: %w", err)
Expand All @@ -138,10 +139,8 @@ func (a *Authenticator) CheckMembership(token string, login string) (bool, error
return false, fmt.Errorf("could not close body: %w", err)
}

for i := range githubRes {
if githubRes[i].Login == login {
return true, nil
}
if githubRes.Message != "" {
return true, nil
}

return false, nil
Expand Down
3 changes: 3 additions & 0 deletions auth/whoami.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package auth

import (
"fmt"
"log/slog"
"net/http"

Expand All @@ -9,6 +10,8 @@ import (

func WhoAmIHandler(res http.ResponseWriter, req *http.Request) {
user := GetUser(req)
token, _ := req.Context().Value("token").(string)
fmt.Println(token)
if err := util.WriteJson(res, user); err != nil {
_ = util.WriteError(res, http.StatusInternalServerError, "")
slog.Error("could not encode json:", "error", err)
Expand Down

0 comments on commit d0ea9d2

Please sign in to comment.