Select Git revision
gitlab.go 3.23 KiB
package gitlab
import (
"errors"
"strconv"
"github.com/nsheridan/cashier/server/auth"
"github.com/nsheridan/cashier/server/config"
gitlabapi "github.com/xanzy/go-gitlab"
"golang.org/x/oauth2"
)
const (
name = "gitlab"
)
// Config is an implementation of `auth.Provider` for authenticating using a
// Gitlab account.
type Config struct {
config *oauth2.Config
baseurl string
group string
whitelist map[string]bool
allusers bool
}
// New creates a new Gitlab provider from a configuration.
func New(c *config.Auth) (*Config, error) {
uw := make(map[string]bool)
for _, u := range c.UsersWhitelist {
uw[u] = true
}
allUsers, _ := strconv.ParseBool(c.ProviderOpts["allusers"])
if !allUsers && c.ProviderOpts["group"] == "" && len(uw) == 0 {
return nil, errors.New("gitlab_opts group and the users whitelist must not be both empty if allusers isn't true")
}
siteURL := "https://gitlab.com/"
if c.ProviderOpts["siteurl"] != "" {
siteURL = c.ProviderOpts["siteurl"]
if siteURL[len(siteURL)-1] != '/' {
return nil, errors.New("gitlab_opts siteurl must end in /")
}
} else {
if allUsers {
return nil, errors.New("gitlab_opts if allusers is set, siteurl must be set")
}
}
return &Config{
config: &oauth2.Config{
ClientID: c.OauthClientID,
ClientSecret: c.OauthClientSecret,
RedirectURL: c.OauthCallbackURL,
Endpoint: oauth2.Endpoint{
AuthURL: siteURL + "oauth/authorize",
TokenURL: siteURL + "oauth/token",
},
Scopes: []string{
"api",
},
},
group: c.ProviderOpts["group"],
whitelist: uw,
allusers: allUsers,
baseurl: siteURL + "api/v3/",
}, nil
}
// Name returns the name of the provider.