Skip to content
Snippets Groups Projects
Select Git revision
  • 8a9cd6cd46d39c26e87db7f800bbbfe0f4c7d16e
  • 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 5.93 KiB
    package gitlab
    
    import (
    	"bytes"
    	"encoding/json"
    	"errors"
    	"fmt"
    	"io"
    	"log"
    	"net/http"
    	"strconv"
    
    	"github.com/nsheridan/cashier/server/config"
    	"github.com/nsheridan/cashier/server/metrics"
    
    	"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
    	group     string
    	whitelist map[string]bool
    	allusers  bool
    	apiurl    string
    	log       bool
    }
    
    // Note on Gitlab REST API calls.  We don't parse errors because it's
    // kind of a pain:
    // https://gitlab.com/help/api/README.md#data-validation-and-error-reporting
    // The two v4 api calls used are /user and /groups/:group/members/:uid
    // https://gitlab.com/help/api/users.md#for-normal-users-1
    // https://gitlab.com/help/api/members.md#get-a-member-of-a-group-or-project
    type serviceUser struct {
    	ID       int    `json:"id"`
    	Username string `json:"username"`
    	Email    string `json:"email"`
    }
    
    type serviceGroupMember struct {
    	ID          int    `json:"id"`
    	State       string `json:"state"`
    	AccessLevel int    `json:"access_level"`
    }
    
    func (c *Config) logMsg(message string) {
    	if c.log {
    		log.Print(message)
    	}
    }
    
    // A new oauth2 http client.
    func (c *Config) newClient(token *oauth2.Token) *http.Client {
    	return c.config.Client(oauth2.NoContext, token)
    }
    
    // Gets info on the current user.
    func (c *Config) getUser(token *oauth2.Token) *serviceUser {
    	client := c.newClient(token)
    	url := c.apiurl + "user"
    	resp, err := client.Get(url)
    	if err != nil {
    		return nil
    	}