...

Source file src/golang.org/x/oauth2/hipchat/hipchat.go

Documentation: golang.org/x/oauth2/hipchat

     1  // Copyright 2016 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // Package hipchat provides constants for using OAuth2 to access HipChat.
     6  package hipchat // import "golang.org/x/oauth2/hipchat"
     7  
     8  import (
     9  	"encoding/json"
    10  	"errors"
    11  
    12  	"golang.org/x/oauth2"
    13  	"golang.org/x/oauth2/clientcredentials"
    14  )
    15  
    16  // Endpoint is HipChat's OAuth 2.0 endpoint.
    17  var Endpoint = oauth2.Endpoint{
    18  	AuthURL:  "https://www.hipchat.com/users/authorize",
    19  	TokenURL: "https://api.hipchat.com/v2/oauth/token",
    20  }
    21  
    22  // ServerEndpoint returns a new oauth2.Endpoint for a HipChat Server instance
    23  // running on the given domain or host.
    24  func ServerEndpoint(host string) oauth2.Endpoint {
    25  	return oauth2.Endpoint{
    26  		AuthURL:  "https://" + host + "/users/authorize",
    27  		TokenURL: "https://" + host + "/v2/oauth/token",
    28  	}
    29  }
    30  
    31  // ClientCredentialsConfigFromCaps generates a Config from a HipChat API
    32  // capabilities descriptor. It does not verify the scopes against the
    33  // capabilities document at this time.
    34  //
    35  // For more information see: https://www.hipchat.com/docs/apiv2/method/get_capabilities
    36  func ClientCredentialsConfigFromCaps(capsJSON []byte, clientID, clientSecret string, scopes ...string) (*clientcredentials.Config, error) {
    37  	var caps struct {
    38  		Caps struct {
    39  			Endpoint struct {
    40  				TokenURL string `json:"tokenUrl"`
    41  			} `json:"oauth2Provider"`
    42  		} `json:"capabilities"`
    43  	}
    44  
    45  	if err := json.Unmarshal(capsJSON, &caps); err != nil {
    46  		return nil, err
    47  	}
    48  
    49  	// Verify required fields.
    50  	if caps.Caps.Endpoint.TokenURL == "" {
    51  		return nil, errors.New("oauth2/hipchat: missing OAuth2 token URL in the capabilities descriptor JSON")
    52  	}
    53  
    54  	return &clientcredentials.Config{
    55  		ClientID:     clientID,
    56  		ClientSecret: clientSecret,
    57  		Scopes:       scopes,
    58  		TokenURL:     caps.Caps.Endpoint.TokenURL,
    59  	}, nil
    60  }
    61  

View as plain text