Beispiel #1
0
func (api *httpAPI) NewCredential(w http.ResponseWriter, req *http.Request, params httprouter.Params) {
	creds := &Credential{}
	if err := httphelper.DecodeJSON(req, &creds); err != nil {
		httphelper.Error(w, err)
		return
	}
	if creds.Type == "azure" {
		oauthCreds := make([]*OAuthCredential, 0, 2)
		for _, resource := range []string{azure.JSONAPIResource, azure.XMLAPIResource} {
			token, err := azure.OAuth2Config(creds.ID, creds.Endpoint, resource).Exchange(oauth2.NoContext, creds.Secret)
			if err != nil {
				httphelper.Error(w, err)
				return
			}
			oauthCreds = append(oauthCreds, &OAuthCredential{
				ClientID:     creds.ID,
				AccessToken:  token.AccessToken,
				RefreshToken: token.RefreshToken,
				ExpiresAt:    &token.Expiry,
				Scope:        resource,
			})
		}
		creds.Secret = ""
		creds.OAuthCreds = oauthCreds
	}
	if err := api.Installer.SaveCredentials(creds); err != nil {
		if err == credentialExistsError {
			httphelper.ObjectExistsError(w, err.Error())
			return
		}
		httphelper.Error(w, err)
		return
	}
	w.WriteHeader(200)
}
Beispiel #2
0
func (i *Installer) azureClient(creds *Credential) *azure.Client {
	var azureJSONOAuthClient *http.Client
	var azureXMLOAuthClient *http.Client
	for _, oc := range creds.OAuthCreds {
		ctx := context.WithValue(oauth2.NoContext, oauth2.TokenRefreshNotifier, i.azureTokenRefreshHandler(oc.ClientID, oc.Scope))
		token := &oauth2.Token{
			AccessToken:  oc.AccessToken,
			RefreshToken: oc.RefreshToken,
			Expiry:       *oc.ExpiresAt,
		}
		switch oc.Scope {
		case azure.JSONAPIResource:
			azureJSONOAuthClient = azure.OAuth2Config(oc.ClientID, creds.Endpoint, oc.Scope).Client(ctx, token)
		case azure.XMLAPIResource:
			azureXMLOAuthClient = azure.OAuth2Config(oc.ClientID, creds.Endpoint, oc.Scope).Client(ctx, token)
		}
	}
	return azure.NewClient(azureJSONOAuthClient, azureXMLOAuthClient)
}