Skip to content

Commit

Permalink
Handler -> Provider, Url -> URL, lint comments
Browse files Browse the repository at this point in the history
  • Loading branch information
bnfinet committed Apr 1, 2020
1 parent e9bd691 commit 8cd1fd5
Show file tree
Hide file tree
Showing 12 changed files with 73 additions and 56 deletions.
6 changes: 4 additions & 2 deletions handlers/adfs/adfs.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import (
"strings"
)

type Handler struct{}
// Provider provider specific functions
type Provider struct{}

type adfsTokenRes struct {
AccessToken string `json:"access_token"`
Expand All @@ -27,8 +28,9 @@ var (
log = cfg.Cfg.Logger
)

// GetUserInfo provider specific call to get userinfomation
// More info: https://docs.microsoft.com/en-us/windows-server/identity/ad-fs/overview/ad-fs-scenarios-for-developers#supported-scenarios
func (Handler) GetUserInfo(r *http.Request, user *structs.User, customClaims *structs.CustomClaims, ptokens *structs.PTokens) (rerr error) {
func (Provider) GetUserInfo(r *http.Request, user *structs.User, customClaims *structs.CustomClaims, ptokens *structs.PTokens) (rerr error) {
code := r.URL.Query().Get("code")
log.Debugf("code: %s", code)

Expand Down
11 changes: 6 additions & 5 deletions handlers/github/github.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,19 @@ import (
"strings"
)

type Handler struct {
PrepareTokensAndClient func(*http.Request, *structs.PTokens, bool) (error, *http.Client, *oauth2.Token)
// Provider provider specific functions
type Provider struct {
PrepareTokensAndClient func(*http.Request, *structs.PTokens, bool) (*http.Client, *oauth2.Token, error)
}

var (
log = cfg.Cfg.Logger
)

// github
// GetUserInfo github user info, calls github api for org and teams
// https://developer.github.com/apps/building-integrations/setting-up-and-registering-oauth-apps/about-authorization-options-for-oauth-apps/
func (me Handler) GetUserInfo(r *http.Request, user *structs.User, customClaims *structs.CustomClaims, ptokens *structs.PTokens) (rerr error) {
err, client, ptoken := me.PrepareTokensAndClient(r, ptokens, true)
func (me Provider) GetUserInfo(r *http.Request, user *structs.User, customClaims *structs.CustomClaims, ptokens *structs.PTokens) (rerr error) {
client, ptoken, err := me.PrepareTokensAndClient(r, ptokens, true)
if err != nil {
// http.Error(w, err.Error(), http.StatusBadRequest)
return err
Expand Down
28 changes: 14 additions & 14 deletions handlers/github/github_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,10 @@ func urlEquals(value string) ReqMatcher {
}
}

func assertUrlCalled(t *testing.T, url string) {
func assertURLCalled(t *testing.T, url string) {
found := false
for _, requested_url := range requests {
if requested_url == url {
for _, requestedURL := range requests {
if requestedURL == url {
found = true
break
}
Expand Down Expand Up @@ -132,8 +132,8 @@ func TestGetOrgMembershipStateFromGitHubNotFound(t *testing.T) {
assert.Nil(t, err)
assert.False(t, isMember)

expectedOrgMembershipUrl := "https://api.github.com/orgs/myorg/members/" + user.Username + "?access_token=" + token.AccessToken
assertUrlCalled(t, expectedOrgMembershipUrl)
expectedOrgMembershipURL := "https://api.github.com/orgs/myorg/members/" + user.Username + "?access_token=" + token.AccessToken
assertURLCalled(t, expectedOrgMembershipURL)
}

func TestGetOrgMembershipStateFromGitHubNoOrgAccess(t *testing.T) {
Expand All @@ -148,11 +148,11 @@ func TestGetOrgMembershipStateFromGitHubNoOrgAccess(t *testing.T) {
assert.Nil(t, err)
assert.True(t, isMember)

expectedOrgMembershipUrl := "https://api.github.com/orgs/myorg/members/" + user.Username + "?access_token=" + token.AccessToken
assertUrlCalled(t, expectedOrgMembershipUrl)
expectedOrgMembershipURL := "https://api.github.com/orgs/myorg/members/" + user.Username + "?access_token=" + token.AccessToken
assertURLCalled(t, expectedOrgMembershipURL)

expectedOrgPublicMembershipUrl := "https://api.github.com/orgs/myorg/public_members/" + user.Username
assertUrlCalled(t, expectedOrgPublicMembershipUrl)
expectedOrgPublicMembershipURL := "https://api.github.com/orgs/myorg/public_members/" + user.Username
assertURLCalled(t, expectedOrgPublicMembershipURL)
}

func TestGetUserInfo(t *testing.T) {
Expand All @@ -177,15 +177,15 @@ func TestGetUserInfo(t *testing.T) {
mockResponse(regexMatcher(".*teams.*"), http.StatusOK, map[string]string{}, []byte("{\"state\": \"active\"}"))
mockResponse(regexMatcher(".*members.*"), http.StatusNoContent, map[string]string{}, []byte(""))

handler := Handler{PrepareTokensAndClient: func(_ *http.Request, _ *structs.PTokens, _ bool) (error, *http.Client, *oauth2.Token) {
return nil, client, token
provider := Provider{PrepareTokensAndClient: func(_ *http.Request, _ *structs.PTokens, _ bool) (*http.Client, *oauth2.Token, error) {
return client, token, nil
}}
err := handler.GetUserInfo(nil, user, &structs.CustomClaims{}, &structs.PTokens{})
err := provider.GetUserInfo(nil, user, &structs.CustomClaims{}, &structs.PTokens{})

assert.Nil(t, err)
assert.Equal(t, "myusername", user.Username)
assert.Equal(t, []string{"myOtherOrg", "myorg/myteam"}, user.TeamMemberships)

expectedTeamMembershipUrl := "https://api.github.com/orgs/myorg/teams/myteam/memberships/myusername?access_token=" + token.AccessToken
assertUrlCalled(t, expectedTeamMembershipUrl)
expectedTeamMembershipURL := "https://api.github.com/orgs/myorg/teams/myteam/memberships/myusername?access_token=" + token.AccessToken
assertURLCalled(t, expectedTeamMembershipURL)
}
8 changes: 5 additions & 3 deletions handlers/google/google.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@ import (
"net/http"
)

type Handler struct{}
// Provider provider specific functions
type Provider struct{}

var (
log = cfg.Cfg.Logger
)

func (Handler) GetUserInfo(r *http.Request, user *structs.User, customClaims *structs.CustomClaims, ptokens *structs.PTokens) (rerr error) {
err, client, _ := common.PrepareTokensAndClient(r, ptokens, true)
// GetUserInfo provider specific call to get userinfomation
func (Provider) GetUserInfo(r *http.Request, user *structs.User, customClaims *structs.CustomClaims, ptokens *structs.PTokens) (rerr error) {
client, _, err := common.PrepareTokensAndClient(r, ptokens, true)
if err != nil {
return err
}
Expand Down
26 changes: 13 additions & 13 deletions handlers/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ type AuthError struct {
JWT string
}

// Handler each Provider must support GetuserInfo
type Handler interface {
// Provider each Provider must support GetuserInfo
type Provider interface {
GetUserInfo(r *http.Request, user *structs.User, customClaims *structs.CustomClaims, ptokens *structs.PTokens) error
}

Expand Down Expand Up @@ -95,7 +95,7 @@ func loginURL(r *http.Request, state string) string {
lurl = cfg.OAuthClient.AuthCodeURL(state)
}
}
// log.Debugf("loginUrl %s", url)
// log.Debugf("loginURL %s", url)
return lurl
}

Expand Down Expand Up @@ -533,27 +533,27 @@ func CallbackHandler(w http.ResponseWriter, r *http.Request) {
}

func getUserInfo(r *http.Request, user *structs.User, customClaims *structs.CustomClaims, ptokens *structs.PTokens) error {
return getHandler().GetUserInfo(r, user, customClaims, ptokens)
return getProvider().GetUserInfo(r, user, customClaims, ptokens)
}

func getHandler() Handler {
func getProvider() Provider {
switch cfg.GenOAuth.Provider {
case cfg.Providers.IndieAuth:
return indieauth.Handler{}
return indieauth.Provider{}
case cfg.Providers.ADFS:
return adfs.Handler{}
return adfs.Provider{}
case cfg.Providers.HomeAssistant:
return homeassistant.Handler{}
return homeassistant.Provider{}
case cfg.Providers.OpenStax:
return openstax.Handler{}
return openstax.Provider{}
case cfg.Providers.Google:
return google.Handler{}
return google.Provider{}
case cfg.Providers.GitHub:
return github.Handler{PrepareTokensAndClient: common.PrepareTokensAndClient}
return github.Provider{PrepareTokensAndClient: common.PrepareTokensAndClient}
case cfg.Providers.Nextcloud:
return nextcloud.Handler{}
return nextcloud.Provider{}
case cfg.Providers.OIDC:
return openid.Handler{}
return openid.Provider{}
default:
log.Error("we don't know how to look up the user info")
return nil
Expand Down
8 changes: 5 additions & 3 deletions handlers/homeassistant/homeassistant.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ import (
"net/http"
)

type Handler struct{}
// Provider provider specific functions
type Provider struct{}

// GetUserInfo provider specific call to get userinfomation
// More info: https://developers.home-assistant.io/docs/en/auth_api.html
func (Handler) GetUserInfo(r *http.Request, user *structs.User, customClaims *structs.CustomClaims, ptokens *structs.PTokens) (rerr error) {
err, _, providerToken := common.PrepareTokensAndClient(r, ptokens, false)
func (Provider) GetUserInfo(r *http.Request, user *structs.User, customClaims *structs.CustomClaims, ptokens *structs.PTokens) (rerr error) {
_, providerToken, err := common.PrepareTokensAndClient(r, ptokens, false)
if err != nil {
return err
}
Expand Down
6 changes: 4 additions & 2 deletions handlers/indieauth/indieauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ import (
"net/http"
)

type Handler struct{}
// Provider provider specific functions
type Provider struct{}

var (
log = cfg.Cfg.Logger
)

func (Handler) GetUserInfo(r *http.Request, user *structs.User, customClaims *structs.CustomClaims, ptokens *structs.PTokens) (rerr error) {
// GetUserInfo provider specific call to get userinfomation
func (Provider) GetUserInfo(r *http.Request, user *structs.User, customClaims *structs.CustomClaims, ptokens *structs.PTokens) (rerr error) {
// indieauth sends the "me" setting in json back to the callback, so just pluck it from the callback
code := r.URL.Query().Get("code")
log.Errorf("ptoken.AccessToken: %s", code)
Expand Down
8 changes: 5 additions & 3 deletions handlers/nextcloud/nextcloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@ import (
"net/http"
)

type Handler struct{}
// Provider provider specific functions
type Provider struct{}

var (
log = cfg.Cfg.Logger
)

func (Handler) GetUserInfo(r *http.Request, user *structs.User, customClaims *structs.CustomClaims, ptokens *structs.PTokens) (rerr error) {
err, client, _ := common.PrepareTokensAndClient(r, ptokens, true)
// GetUserInfo provider specific call to get userinfomation
func (Provider) GetUserInfo(r *http.Request, user *structs.User, customClaims *structs.CustomClaims, ptokens *structs.PTokens) (rerr error) {
client, _, err := common.PrepareTokensAndClient(r, ptokens, true)
if err != nil {
return err
}
Expand Down
8 changes: 5 additions & 3 deletions handlers/openid/openid.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@ import (
"net/http"
)

type Handler struct{}
// Provider provider specific functions
type Provider struct{}

var (
log = cfg.Cfg.Logger
)

func (Handler) GetUserInfo(r *http.Request, user *structs.User, customClaims *structs.CustomClaims, ptokens *structs.PTokens) (rerr error) {
err, client, _ := common.PrepareTokensAndClient(r, ptokens, true)
// GetUserInfo provider specific call to get userinfomation
func (Provider) GetUserInfo(r *http.Request, user *structs.User, customClaims *structs.CustomClaims, ptokens *structs.PTokens) (rerr error) {
client, _, err := common.PrepareTokensAndClient(r, ptokens, true)
if err != nil {
return err
}
Expand Down
8 changes: 5 additions & 3 deletions handlers/openstax/openstax.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@ import (
"net/http"
)

type Handler struct{}
// Provider provider specific functions
type Provider struct{}

var (
log = cfg.Cfg.Logger
)

func (Handler) GetUserInfo(r *http.Request, user *structs.User, customClaims *structs.CustomClaims, ptokens *structs.PTokens) (rerr error) {
err, client, _ := common.PrepareTokensAndClient(r, ptokens, false)
// GetUserInfo provider specific call to get userinfomation
func (Provider) GetUserInfo(r *http.Request, user *structs.User, customClaims *structs.CustomClaims, ptokens *structs.PTokens) (rerr error) {
client, _, err := common.PrepareTokensAndClient(r, ptokens, false)
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/domains/domains_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ func TestIsUnderManagement(t *testing.T) {
func TestMatches(t *testing.T) {
// Full email should not be accepted
assert.Equal(t, "", Matches("test@vouch.github.io"))

assert.Equal(t, "vouch.github.io", Matches("vouch.github.io"))
assert.Equal(t, "vouch.github.io", Matches("sub.vouch.github.io"))
assert.Equal(t, "", Matches("a-different-vouch.github.io"))

assert.Equal(t, "", Matches("mydomain.com"))

assert.Equal(t, "test.mydomain.com", Matches("test.mydomain.com"))
assert.Equal(t, "sub.test.mydomain.com", Matches("sub.test.mydomain.com"))
assert.Equal(t, "sub.test.mydomain.com", Matches("subsub.sub.test.mydomain.com"))
Expand Down
8 changes: 5 additions & 3 deletions pkg/structs/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ type GitHubUser struct {
// jwt.StandardClaims
}

// GitHubTeamMembershipState for GitHub team api call
type GitHubTeamMembershipState struct {
State string `json:"state"`
}
Expand Down Expand Up @@ -131,20 +132,21 @@ func (u *OpenStaxUser) PrepareUserData() {
// Ocs used for NextcloudUser
type Ocs struct {
Data struct {
UserId string `json:"id"`
UserID string `json:"id"`
Email string `json:"email"`
} `json:"data"`
}

// User of Nextcloud retreived from ocs endpoint
// NextcloudUser User of Nextcloud retreived from ocs endpoint
type NextcloudUser struct {
User
Ocs Ocs `json:"ocs"`
}

// PrepareUserData NextcloudUser
func (u *NextcloudUser) PrepareUserData() {
if u.Username == "" {
u.Username = u.Ocs.Data.UserId
u.Username = u.Ocs.Data.UserID
u.Email = u.Ocs.Data.Email
}
}
Expand Down

0 comments on commit 8cd1fd5

Please sign in to comment.