本文整理汇总了Golang中golang.org/x/oauth2/clientcredentials.Config.Token方法的典型用法代码示例。如果您正苦于以下问题:Golang Config.Token方法的具体用法?Golang Config.Token怎么用?Golang Config.Token使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类golang.org/x/oauth2/clientcredentials.Config
的用法示例。
在下文中一共展示了Config.Token方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestClientGrantType
func TestClientGrantType(t *testing.T) {
router := mux.NewRouter()
handler.SetRoutes(router, mockAuthorization("", new(jwt.Token)))
ts := httptest.NewServer(router)
defer ts.Close()
for k, c := range []*struct {
config *oauth2.Config
pass bool
}{
{configs["working"], true},
{configs["voidSecret"], false},
{configs["voidID"], false},
{configs["working"], true},
} {
conf := clientcredentials.Config{
ClientID: c.config.ClientID,
ClientSecret: c.config.ClientSecret,
TokenURL: ts.URL + c.config.Endpoint.TokenURL,
Scopes: c.config.Scopes,
}
_, err := conf.Token(oauth2.NoContext)
if c.pass {
assert.Nil(t, err, "Case %d\n%v", k, conf)
} else {
assert.NotNil(t, err, "Case %d\n%v", k, conf)
}
}
}
示例2: apiClientAuth
func (authd *authdetails) apiClientAuth() (*brightbox.Client, error) {
conf := clientcredentials.Config{
ClientID: authd.APIClient,
ClientSecret: authd.APISecret,
Scopes: infrastructureScope,
TokenURL: authd.tokenURL(),
}
log.Printf("[DEBUG] Obtaining API client authorisation for client %s", authd.APIClient)
oauthConnection := conf.Client(oauth2.NoContext)
if authd.currentToken == nil {
log.Printf("[DEBUG] Retrieving auth token for %s", conf.ClientID)
token, err := conf.Token(oauth2.NoContext)
if err != nil {
return nil, err
}
authd.currentToken = token
}
return brightbox.NewClient(authd.APIURL, authd.Account, oauthConnection)
}
示例3: OAuth2Client
func (c *Config) OAuth2Client(cmd *cobra.Command) *http.Client {
c.Lock()
defer c.Unlock()
if c.oauth2Client != nil {
return c.oauth2Client
}
oauthConfig := clientcredentials.Config{
ClientID: c.ClientID,
ClientSecret: c.ClientSecret,
TokenURL: pkg.JoinURLStrings(c.ClusterURL, "/oauth2/token"),
Scopes: []string{
"core",
"hydra",
},
}
ctx := context.Background()
if ok, _ := cmd.Flags().GetBool("skip-tls-verify"); ok {
fmt.Println("Warning: Skipping TLS Certificate Verification.")
ctx = context.WithValue(context.Background(), oauth2.HTTPClient, &http.Client{Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}})
}
_, err := oauthConfig.Token(ctx)
if err != nil {
fmt.Printf("Could not authenticate, because: %s\n", err)
fmt.Println("Did you forget to log on? Run `hydra connect`.")
fmt.Println("Did you run Hydra without a valid TLS certificate? Make sure to use the `--skip-tls-verify` flag.")
fmt.Println("Did you know you can skip `hydra connect` when running `hydra host --dangerous-auto-logon`? DO NOT use this flag in production!")
os.Exit(1)
}
c.oauth2Client = oauthConfig.Client(ctx)
return c.oauth2Client
}
示例4: TestIntrospect
func TestIntrospect(t *testing.T) {
router := mux.NewRouter()
handler.SetRoutes(router, mockAuthorization("subject", &jwt.Token{Valid: true}))
ts := httptest.NewServer(router)
defer ts.Close()
config := configs["working"]
user := logins["working"]
clientConfig := clientcredentials.Config{
ClientID: config.ClientID,
ClientSecret: config.ClientSecret,
TokenURL: ts.URL + config.Endpoint.TokenURL,
Scopes: config.Scopes,
}
config.Endpoint = oauth2.Endpoint{AuthURL: ts.URL + "/oauth2/auth", TokenURL: ts.URL + "/oauth2/token"}
access, err := clientConfig.Token(oauth2.NoContext)
require.Nil(t, err)
verify, err := config.PasswordCredentialsToken(oauth2.NoContext, user.Username, user.Password)
require.Nil(t, err)
for k, c := range []*struct {
accessToken string
code int
pass bool
clientID string
clientSecret string
}{
{
accessToken: verify.AccessToken,
code: http.StatusUnauthorized,
pass: false,
clientSecret: "not-working",
},
{
accessToken: verify.AccessToken,
code: http.StatusUnauthorized,
pass: false,
clientID: "not-existing",
},
{
accessToken: verify.AccessToken,
code: http.StatusOK,
pass: true,
},
{
accessToken: access.AccessToken,
code: http.StatusOK,
pass: true,
},
{
accessToken: "",
code: http.StatusOK,
pass: false,
},
{
accessToken: " ",
code: http.StatusOK,
pass: false,
},
{
accessToken: "invalid",
code: http.StatusOK,
pass: false,
},
//
{
accessToken: "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.e30.FvuwHdEjgGxPAyVUb-eqtiPl2gycU9WOHNzwpFKcpdN_QkXkBUxU3qFl3lLBaMzIuP_GjXLXcJZFhyQ2Ne3kfWuZSGLmob0Og8B4lAy7CA7iwpji2R3aUcwBwbJ41IJa__F8fMRz0dRDwhyrBKD-9y4TfV_-yZuzBZxq0UdjX6IdpzsdetphBSIZkPij5MY3thRwC-X_gXyIXi4-G2_CjRrV5lCGnPJrDbLqPCYqS71wK9NEsz_B8p5ENmwad8vZe4fEFR7XsqJrhPjbEVGeLpzSz0AOGp4G1iyvv1sdu4M3Y8KSSGYnZ8lXNGyi8QeUr374Y6XgJ5N5TVLWI2cMxg",
code: http.StatusOK,
pass: false,
},
// "exp": 12345
{
accessToken: "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJtYXgiLCJleHAiOjEyMzQ1fQ.0w2dienBCvgfbhLjmK04fFKqf2oFRMNoKS0A3zHBpU_yN22utC_gAvcFwKiMffebtHah7rgldnPqNZaNhfnEM1PxNFh46vXO5LNZDHt5sNZqeBtZ1Q7ORkZsAtIp97mtZMxufn0VBqJTRYxyDrEzH9Mo1OpXuPTzDP87n-p_Xdbpj5YccZU6TZ11eLs9NvuYu_A2HClKrGbCeaHFAGVWVaoSZ_TvjGqyBI-XoGzuCEBoj6NFTHxZpbNeKhVTTwXHv2sUn09gZ_ErmbPZKExV5sCLETktr4ABUXkNtw4xLW6g0EVzC9dRMKxUZO8kCmAJkKHUTinEDjpfX_n8CKRQVQ",
code: http.StatusOK,
pass: false,
},
// {
// "exp": 1924975619,
// "nbf": 1924975619
// }
{
accessToken: "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJtYXgiLCJleHAiOjE5MjQ5NzU2MTksIm5iZiI6MTkyNDk3NTYxOX0.P381fgXq75I1iFBFMA624LgKm-wyous9VV4aQHS2O9kDyCJUejK71-M5owaWkjDOkHFlE7Ju5yknasODNlYsuzB2ujos1xiCuHYjoqivvSPNwrxJMXKMXrtzzk045E_OH1EHd_d9KVmrnA5dd3NLqNdYAoUogrO4TistjpZOv-ABUesiKIOR6SopD2tUxHog4RmFFtBJOt4l9P2aGn4a6LBt5wvBz9wUKak7YzUKMZXsWus-x-RP41bulpsUPEfH4TtgQHOM-VQ5W-EORhH8PClBfUrPyp1H7bgXOjhvCdpf4dfJS59Wf3euq9TXT0axyJ5HErXy3yOwC0E2ggl2iQ",
code: http.StatusOK,
pass: false,
},
// {
// "exp": 1924975619,
// "iat": 1924975619,
// "nbf": 0
// }
{
accessToken: "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJtYXgiLCJleHAiOjE5MjQ5NzU2MTksIm5iZiI6MCwiaWF0IjoxOTI0OTc1NjE5fQ.qwUo8-e9tcg69pv9SJFpMXytJtAZlTJoVZh73bVtpkImZ0G5s_cbzPvccM_LmmHl5rFCpQuwWDSuHME2iyer6-gC2DILGQiXyJ5JhJdAKD4xtSFnV90zu84BF8L4JWqLeIEV13AHTpphfS0tOOOKL6sFYbo4LQVslfRYON28D3iOP-YAKJeorHsZgTNg-7VjPC8w_emDpVoNiWEyON2gHrucKiJlWQJVE_gxLf_n-F29UV1OBi-AjxccCrXMd0pzndZ7zg_7EbaUuOmLStfn2ORkoARaHaw55Sv2vbf_AV0MWsgqPaOlK6GTbfv3sYjB7K9eItWh9o8kDXNM4blqSw",
code: http.StatusOK,
pass: false,
},
// {
// "exp": 1924975619,
// "iat": 1924975619,
//.........这里部分代码省略.........
示例5: init
Short: "Generate an OAuth2 token the client grant type",
Long: "This command uses the CLI's credentials to create an access token.",
Run: func(cmd *cobra.Command, args []string) {
ctx := context.Background()
if ok, _ := cmd.Flags().GetBool("skip-tls-verify"); ok {
fmt.Println("Warning: Skipping TLS Certificate Verification.")
ctx = context.WithValue(context.Background(), oauth2.HTTPClient, &http.Client{Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}})
}
oauthConfig := clientcredentials.Config{
ClientID: c.ClientID,
ClientSecret: c.ClientSecret,
TokenURL: pkg.JoinURLStrings(c.ClusterURL, "/oauth2/token"),
Scopes: []string{
"core",
"hydra",
},
}
t, err := oauthConfig.Token(ctx)
pkg.Must(err, "Could not authenticate, because: %s\n", err)
fmt.Printf("%s", t.AccessToken)
},
}
func init() {
tokenCmd.AddCommand(tokenSelfCmd)
}