Skip to content
Snippets Groups Projects
Select Git revision
  • 9c344a0a95c44ef9cebade7b8a65ac160d9eb900
  • ballinvoher default protected
  • client-http-server-for-token
  • master
  • gitlab-auth-issue
  • windows
  • microsoft
  • message
  • azure_auth
  • prometheus
  • permission-templates
  • no-datastore
  • save-public-keys
  • gitlab-group-level-start
  • v1.1.0
  • v1.0.0
  • v0.1
17 results

gitlab.go

Blame
  • 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.