示例#1
0
文件: client.go 项目: Ablu/drone
func NewClient(ConsumerRSA string, ConsumerKey string, URL string) *oauth.Consumer {
	//TODO: make this configurable
	privateKeyFileContents, err := ioutil.ReadFile(ConsumerRSA)
	log.Info("Tried to read the key")
	if err != nil {
		log.Error(err)
	}

	block, _ := pem.Decode([]byte(privateKeyFileContents))
	privateKey, err := x509.ParsePKCS1PrivateKey(block.Bytes)
	if err != nil {
		log.Error(err)
	}

	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
}
示例#2
0
文件: goxero.go 项目: mhlo/goxero
func NewPrivateConsumer(consumerKey string, rsaKey *rsa.PrivateKey) *XeroConsumer {

	x := XeroConsumer{}
	x.Consumer = oauth.NewRSAConsumer(
		consumerKey,
		rsaKey,
		oauth.ServiceProvider{
			RequestTokenUrl:   XeroRequestTokenURL,
			AuthorizeTokenUrl: XeroAuthorizeTokenURL,
			AccessTokenUrl:    XeroAccessTokenURL,
		})
	return &x
}
示例#3
0
func CreateConsumer(URL string, ConsumerKey string, PrivateKey *rsa.PrivateKey) *oauth.Consumer {
	consumer := oauth.NewRSAConsumer(
		ConsumerKey,
		PrivateKey,
		oauth.ServiceProvider{
			RequestTokenUrl:   fmt.Sprintf(requestTokenURL, URL),
			AuthorizeTokenUrl: fmt.Sprintf(authorizeTokenURL, URL),
			AccessTokenUrl:    fmt.Sprintf(accessTokenURL, URL),
			HttpMethod:        "POST",
		})
	consumer.HttpClient = &http.Client{
		Transport: &http.Transport{
			TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
		},
	}
	return consumer
}
示例#4
0
func main() {
	var consumerKey *string = flag.String(
		"consumerkey",
		"",
		"Consumer Key from service provider.")

	var privateKeyFile *string = flag.String(
		"privatekeyfile",
		"",
		"File name of a PEM encoded private key.")

	var jiraUrl *string = flag.String(
		"jiraurl",
		"",
		"Base URL of the Jira service.")

	flag.Parse()

	if len(*consumerKey) == 0 || len(*privateKeyFile) == 0 || len(*jiraUrl) == 0 {
		fmt.Println("You must set the --consumerkey, --privatekeyfile and --jiraurl flags.")
		fmt.Println("---")
		Usage()
		os.Exit(1)
	}

	privateKeyFileContents, err := ioutil.ReadFile(*privateKeyFile)
	if err != nil {
		log.Fatal(err)
	}

	block, _ := pem.Decode([]byte(privateKeyFileContents))
	privateKey, err := x509.ParsePKCS1PrivateKey(block.Bytes)
	if err != nil {
		log.Fatal(err)
	}

	c := oauth.NewRSAConsumer(
		*consumerKey,
		privateKey,
		oauth.ServiceProvider{
			RequestTokenUrl:   *jiraUrl + "/plugins/servlet/oauth/request-token",
			AuthorizeTokenUrl: *jiraUrl + "/plugins/servlet/oauth/authorize",
			AccessTokenUrl:    *jiraUrl + "/plugins/servlet/oauth/access-token",
			HttpMethod:        "POST",
		})

	c.Debug(true)

	c.HttpClient = &http.Client{
		Transport: &http.Transport{
			TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
		},
	}

	requestToken, url, err := c.GetRequestTokenAndUrl("oob")
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println("(1) Go to: " + url)
	fmt.Println("(2) Grant access, you should get back a verification code.")
	fmt.Println("(3) Enter that verification code here: ")

	verificationCode := ""
	fmt.Scanln(&verificationCode)

	accessToken, err := c.AuthorizeToken(requestToken, verificationCode)
	if err != nil {
		log.Fatal(err)
	}

	client, err := c.MakeHttpClient(accessToken)
	if err != nil {
		log.Fatal(err)
	}

	response, err := client.Get(*jiraUrl + "/rest/api/2/issue/BULK-1")
	if err != nil {
		log.Fatal(err)
	}
	defer response.Body.Close()

	bits, err := ioutil.ReadAll(response.Body)
	fmt.Println("Data: " + string(bits))

}