open-vault/builtin/credential/github/backend.go

84 lines
1.8 KiB
Go
Raw Normal View History

2015-04-01 22:46:21 +00:00
package github
import (
"github.com/google/go-github/github"
"github.com/hashicorp/go-cleanhttp"
2015-04-01 22:46:21 +00:00
"github.com/hashicorp/vault/logical"
"github.com/hashicorp/vault/logical/framework"
"golang.org/x/oauth2"
)
func Factory(conf *logical.BackendConfig) (logical.Backend, error) {
return Backend().Setup(conf)
2015-04-01 22:46:21 +00:00
}
func Backend() *framework.Backend {
var b backend
b.Map = &framework.PolicyMap{
PathMap: framework.PathMap{
Name: "teams",
},
2015-04-01 22:46:21 +00:00
DefaultKey: "default",
}
b.Backend = &framework.Backend{
2015-04-04 19:18:33 +00:00
Help: backendHelp,
2015-04-01 22:46:21 +00:00
PathsSpecial: &logical.Paths{
Root: []string{
"config",
},
Unauthenticated: []string{
"login",
},
},
Paths: append([]*framework.Path{
2015-10-02 17:33:19 +00:00
pathConfig(&b),
2015-04-01 22:46:21 +00:00
pathLogin(&b),
}, b.Map.Paths()...),
2015-10-02 17:33:19 +00:00
AuthRenew: b.pathLoginRenew,
2015-04-01 22:46:21 +00:00
}
return b.Backend
}
type backend struct {
*framework.Backend
Map *framework.PolicyMap
}
// Client returns the GitHub client to communicate to GitHub via the
// configured settings.
func (b *backend) Client(token string) (*github.Client, error) {
tc := cleanhttp.DefaultClient()
2015-04-01 22:46:21 +00:00
if token != "" {
tc = oauth2.NewClient(oauth2.NoContext, &tokenSource{Value: token})
}
return github.NewClient(tc), nil
}
// tokenSource is an oauth2.TokenSource implementation.
type tokenSource struct {
Value string
}
func (t *tokenSource) Token() (*oauth2.Token, error) {
return &oauth2.Token{AccessToken: t.Value}, nil
}
2015-04-04 19:18:33 +00:00
const backendHelp = `
The GitHub credential provider allows authentication via GitHub.
Users provide a personal access token to log in, and the credential
provider verifies they're part of the correct organization and then
maps the user to a set of Vault policies according to the teams they're
part of.
After enabling the credential provider, use the "config" route to
configure it.
`