示例#1
0
// ExchangeAppKeyForToken enables authentication with the App Access Key
func (c *Component) ExchangeAppKeyForToken(appID, key string) (string, error) {
	issuerID := keys.KeyIssuer(key)
	if issuerID == "" {
		// Take the first configured auth server
		for k := range c.Config.AuthServers {
			issuerID = k
			break
		}
		key = fmt.Sprintf("%s.%s", issuerID, key)
	}
	issuer, ok := c.Config.AuthServers[issuerID]
	if !ok {
		return "", fmt.Errorf("Auth server %s not registered", issuer)
	}

	srv, _ := parseAuthServer(issuer)

	oauth := oauth.OAuth(srv.url, &oauth.Client{
		ID:     srv.username,
		Secret: srv.password,
	})

	token, err := oauth.ExchangeAppKeyForToken(appID, key)
	if err != nil {
		return "", err
	}

	return token.AccessToken, nil
}
示例#2
0
// getOAuth gets the OAuth client
func getOAuth() *oauth.Config {
	return oauth.OAuth(viper.GetString("auth-server"), &oauth.Client{
		ID:     "ttnctl",
		Secret: "ttnctl",
		ExtraHeaders: map[string]string{
			"User-Agent": GetUserAgent(),
		},
	})
}