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 README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ auth:
# - yourdomain.com # domain of your Google App (Google)
# - example@gmail.com # specific email address (same as above)
# - your_company_org # organization name (GitHub)
# - your_company_org/your_team #organization team name (GitHub)

# document root for static files
htdocs: ./
Expand Down Expand Up @@ -91,7 +92,7 @@ auth:
# restrict user request. (optional)
restrictions:
- foo_organization
- bar_organization
- bar_organization/bar_team
```

#### github:e support
Expand Down
131 changes: 102 additions & 29 deletions authenticator.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,55 +131,128 @@ type GitHubAuth struct {
*BaseAuth
}

type OrgTeam struct {
organization string
team string
}

func (a *GitHubAuth) Authenticate(organizations []string, c martini.Context, tokens oauth2.Tokens, w http.ResponseWriter, r *http.Request) {
if len(organizations) > 0 {
req, err := http.NewRequest("GET", fmt.Sprintf("%s/user/orgs", a.conf.Auth.Info.ApiEndpoint), nil)
if err != nil {
log.Printf("failed to create a request to retrieve organizations: %s", err)
forbidden(w)
return
var orgSlice []OrgTeam
var teamSlice []OrgTeam
for _, org := range organizations {
var orgArray = strings.Split(org, "/")
if len(orgArray) > 1 {
teamSlice = append(teamSlice, OrgTeam{organization: orgArray[0], team: orgArray[1]})
} else {
orgSlice = append(orgSlice, OrgTeam{organization: orgArray[0]})
}
}

req.SetBasicAuth(tokens.Access(), "x-oauth-basic")
if len(orgSlice) > 0 {
req, err := http.NewRequest("GET", fmt.Sprintf("%s/user/orgs", a.conf.Auth.Info.ApiEndpoint), nil)
if err != nil {
log.Printf("failed to create a request to retrieve organizations: %s", err)
forbidden(w)
return
}

client := http.Client{}
res, err := client.Do(req)
if err != nil {
log.Printf("failed to retrieve organizations: %s", err)
forbidden(w)
return
}
req.SetBasicAuth(tokens.Access(), "x-oauth-basic")

data, err := ioutil.ReadAll(res.Body)
res.Body.Close()
client := http.Client{}
res, err := client.Do(req)
if err != nil {
log.Printf("failed to retrieve organizations: %s", err)
forbidden(w)
return
}

if err != nil {
log.Printf("failed to read body of GitHub response: %s", err)
forbidden(w)
return
}
data, err := ioutil.ReadAll(res.Body)
res.Body.Close()

var info []map[string]interface{}
if err := json.Unmarshal(data, &info); err != nil {
log.Printf("failed to decode json: %s", err.Error())
forbidden(w)
return
if err != nil {
log.Printf("failed to read body of GitHub response: %s", err)
forbidden(w)
return
}

var info []map[string]interface{}
if err := json.Unmarshal(data, &info); err != nil {
log.Printf("failed to decode json: %s", err.Error())
forbidden(w)
return
}

for _, userOrg := range info {
for _, org := range orgSlice {
if userOrg["login"] == org.organization {
return
}
}
}
}
if len(teamSlice) > 0 {
req, err := http.NewRequest("GET", fmt.Sprintf("%s/user/teams", a.conf.Auth.Info.ApiEndpoint), nil)
if err != nil {
log.Printf("failed to create a request to retrieve teams: %s", err)
forbidden(w)
return
}

req.SetBasicAuth(tokens.Access(), "x-oauth-basic")

client := http.Client{}
res, err := client.Do(req)
if err != nil {
log.Printf("failed to retrieve teams: %s", err)
forbidden(w)
return
}

data, err := ioutil.ReadAll(res.Body)
res.Body.Close()

for _, userOrg := range info {
for _, org := range organizations {
if userOrg["login"] == org {
if err != nil {
log.Printf("failed to read body of GitHub response: %s", err)
forbidden(w)
return
}

var info []map[string]json.RawMessage
if err := json.Unmarshal(data, &info); err != nil {
log.Printf("failed to decode json: %s", err.Error())
forbidden(w)
return
}

for _, userTeam := range info {
var orgInfo map[string]interface{}
if err := json.Unmarshal(userTeam["organization"], &orgInfo); err != nil {
log.Printf("failed to decode json: %s", err.Error())
forbidden(w)
return
}
var teamInfo string
if err := json.Unmarshal(userTeam["slug"], &teamInfo); err != nil {
log.Printf("failed to decode json: %s", err.Error())
forbidden(w)
return
}
for _, team := range teamSlice {
if orgInfo["login"] == team.organization && teamInfo == team.team {
return
}
}
}
}

log.Print("not a member of designated organizations")
log.Print("not a member of designated organizations/teams")
forbidden(w)
return
}
}


func forbidden(w http.ResponseWriter) {
w.WriteHeader(403)
w.Write([]byte("Access denied"))
Expand Down