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

51 lines
998 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) {
serverURL, err := url.Parse(server)
2022-07-20 15:12:41 +00:00
if err != nil {
return nil, err
}
if len(serverURL.Scheme) == 0 {
serverURL.Scheme = "http"
2022-07-20 15:12:41 +00:00
}
// attempt to find system CA certs
certs := syscerts.SystemRootsPool()
tlsConfig := &tls.Config{
RootCAs: certs,
InsecureSkipVerify: false,
MinVersion: tls.VersionTLS12,
2022-07-20 15:12:41 +00:00
}
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,
}
return drone.NewClient(serverURL.String(), authenticator), nil
2022-07-20 15:12:41 +00:00
}