2016-04-19 04:40:49 +00:00
|
|
|
package bitbucketserver
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"crypto/tls"
|
|
|
|
log "github.com/Sirupsen/logrus"
|
|
|
|
"io/ioutil"
|
|
|
|
"encoding/pem"
|
|
|
|
"crypto/x509"
|
|
|
|
"github.com/mrjones/oauth"
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
func NewClient(ConsumerKey string, URL string) *oauth.Consumer{
|
2016-04-19 17:44:19 +00:00
|
|
|
//TODO: make this configurable
|
|
|
|
privateKeyFileContents, err := ioutil.ReadFile("/var/lib/bitbucketserver/private_key.pem")
|
2016-04-19 04:40:49 +00:00
|
|
|
log.Info("Tried to read the key")
|
|
|
|
if err != nil {
|
2016-04-19 16:47:02 +00:00
|
|
|
log.Error(err)
|
2016-04-19 04:40:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
block, _ := pem.Decode([]byte(privateKeyFileContents))
|
|
|
|
privateKey, err := x509.ParsePKCS1PrivateKey(block.Bytes)
|
|
|
|
if err != nil {
|
2016-04-19 16:47:02 +00:00
|
|
|
log.Error(err)
|
2016-04-19 04:40:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
c := oauth.NewRSAConsumer(
|
|
|
|
ConsumerKey,
|
|
|
|
privateKey,
|
|
|
|
oauth.ServiceProvider{
|
|
|
|
RequestTokenUrl: URL + "/plugins/servlet/oauth/request-token",
|
|
|
|
AuthorizeTokenUrl: URL + "/plugins/servlet/oauth/authorize",
|
|
|
|
AccessTokenUrl: URL + "/plugins/servlet/oauth/access-token",
|
|
|
|
HttpMethod: "POST",
|
|
|
|
})
|
|
|
|
c.HttpClient = &http.Client{
|
|
|
|
Transport: &http.Transport{
|
|
|
|
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewClientWithToken(ConsumerKey string, URL string, AccessToken string) *http.Client{
|
|
|
|
NewClient(ConsumerKey, URL)
|
|
|
|
c := NewClient(ConsumerKey, URL)
|
|
|
|
|
|
|
|
var token oauth.AccessToken
|
|
|
|
token.Token = AccessToken
|
|
|
|
client, err := c.MakeHttpClient(&token)
|
|
|
|
if err != nil {
|
2016-04-19 16:47:02 +00:00
|
|
|
log.Error(err)
|
2016-04-19 04:40:49 +00:00
|
|
|
}
|
|
|
|
return client
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|