0
0
mirror of https://github.com/thegeeklab/drone-admin.git synced 2024-11-28 16:40:36 +00:00
drone-admin/admin/client/client.go

49 lines
925 B
Go
Raw Permalink Normal View History

2022-07-20 15:12:41 +00:00
package client
import (
"context"
"crypto/tls"
"net/http"
"net/url"
"time"
"github.com/drone/drone-go/drone"
"github.com/jackspirou/syscerts"
"golang.org/x/oauth2"
)
2022-07-20 15:21:33 +00:00
func New(server, token string) (drone.Client, error) {
2022-07-20 15:12:41 +00:00
s, err := url.Parse(server)
if err != nil {
return nil, err
}
if len(s.Scheme) == 0 {
s.Scheme = "http"
}
// attempt to find system CA certs
certs := syscerts.SystemRootsPool()
tlsConfig := &tls.Config{
RootCAs: certs,
InsecureSkipVerify: false,
}
oauth := new(oauth2.Config)
2022-07-20 15:15:21 +00:00
authenticator := oauth.Client(
2022-07-20 15:12:41 +00:00
context.Background(),
&oauth2.Token{
AccessToken: token,
},
)
2022-07-20 15:15:21 +00:00
authenticator.Timeout, _ = time.ParseDuration("60s")
2022-07-20 15:12:41 +00:00
2022-07-20 15:15:21 +00:00
trans, _ := authenticator.Transport.(*oauth2.Transport)
2022-07-20 15:12:41 +00:00
trans.Base = &http.Transport{
TLSClientConfig: tlsConfig,
Proxy: http.ProxyFromEnvironment,
}
2022-07-20 15:15:21 +00:00
return drone.NewClient(s.String(), authenticator), nil
2022-07-20 15:12:41 +00:00
}