0
0
mirror of https://github.com/thegeeklab/drone-admin.git synced 2024-06-02 17:39:39 +02:00
drone-admin/admin/client/client.go

51 lines
998 B
Go
Raw Normal View History

2022-07-20 17:12:41 +02: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 17:21:33 +02:00
func New(server, token string) (drone.Client, error) {
serverURL, err := url.Parse(server)
2022-07-20 17:12:41 +02:00
if err != nil {
return nil, err
}
if len(serverURL.Scheme) == 0 {
serverURL.Scheme = "http"
2022-07-20 17:12:41 +02:00
}
// attempt to find system CA certs
certs := syscerts.SystemRootsPool()
tlsConfig := &tls.Config{
RootCAs: certs,
InsecureSkipVerify: false,
MinVersion: tls.VersionTLS12,
2022-07-20 17:12:41 +02:00
}
oauth := new(oauth2.Config)
2022-07-20 17:15:21 +02:00
authenticator := oauth.Client(
2022-07-20 17:12:41 +02:00
context.Background(),
&oauth2.Token{
AccessToken: token,
},
)
2022-07-20 17:15:21 +02:00
authenticator.Timeout, _ = time.ParseDuration("60s")
2022-07-20 17:12:41 +02:00
2022-07-20 17:15:21 +02:00
trans, _ := authenticator.Transport.(*oauth2.Transport)
2022-07-20 17:12:41 +02:00
trans.Base = &http.Transport{
TLSClientConfig: tlsConfig,
Proxy: http.ProxyFromEnvironment,
}
return drone.NewClient(serverURL.String(), authenticator), nil
2022-07-20 17:12:41 +02:00
}