github.com/twilio/twilio-go@v1.20.1/rest/verify/v2/services_entities_factors.go (about)

     1  /*
     2   * This code was generated by
     3   * ___ _ _ _ _ _    _ ____    ____ ____ _    ____ ____ _  _ ____ ____ ____ ___ __   __
     4   *  |  | | | | |    | |  | __ |  | |__| | __ | __ |___ |\ | |___ |__/ |__|  | |  | |__/
     5   *  |  |_|_| | |___ | |__|    |__| |  | |    |__] |___ | \| |___ |  \ |  |  | |__| |  \
     6   *
     7   * Twilio - Verify
     8   * This is the public Twilio REST API.
     9   *
    10   * NOTE: This class is auto generated by OpenAPI Generator.
    11   * https://openapi-generator.tech
    12   * Do not edit the class manually.
    13   */
    14  
    15  package openapi
    16  
    17  import (
    18  	"encoding/json"
    19  	"fmt"
    20  	"net/url"
    21  	"strings"
    22  
    23  	"github.com/twilio/twilio-go/client"
    24  )
    25  
    26  // Optional parameters for the method 'CreateNewFactor'
    27  type CreateNewFactorParams struct {
    28  	// The friendly name of this Factor. This can be any string up to 64 characters, meant for humans to distinguish between Factors. For `factor_type` `push`, this could be a device name. For `factor_type` `totp`, this value is used as the “account name” in constructing the `binding.uri` property. At the same time, we recommend avoiding providing PII.
    29  	FriendlyName *string `json:"FriendlyName,omitempty"`
    30  	//
    31  	FactorType *string `json:"FactorType,omitempty"`
    32  	// The algorithm used when `factor_type` is `push`. Algorithm supported: `ES256`
    33  	BindingAlg *string `json:"Binding.Alg,omitempty"`
    34  	// The Ecdsa public key in PKIX, ASN.1 DER format encoded in Base64.  Required when `factor_type` is `push`
    35  	BindingPublicKey *string `json:"Binding.PublicKey,omitempty"`
    36  	// The ID that uniquely identifies your app in the Google or Apple store, such as `com.example.myapp`. It can be up to 100 characters long.  Required when `factor_type` is `push`.
    37  	ConfigAppId *string `json:"Config.AppId,omitempty"`
    38  	//
    39  	ConfigNotificationPlatform *string `json:"Config.NotificationPlatform,omitempty"`
    40  	// For APN, the device token. For FCM, the registration token. It is used to send the push notifications. Must be between 32 and 255 characters long.  Required when `factor_type` is `push`.
    41  	ConfigNotificationToken *string `json:"Config.NotificationToken,omitempty"`
    42  	// The Verify Push SDK version used to configure the factor  Required when `factor_type` is `push`
    43  	ConfigSdkVersion *string `json:"Config.SdkVersion,omitempty"`
    44  	// The shared secret for TOTP factors encoded in Base32. This can be provided when creating the Factor, otherwise it will be generated.  Used when `factor_type` is `totp`
    45  	BindingSecret *string `json:"Binding.Secret,omitempty"`
    46  	// Defines how often, in seconds, are TOTP codes generated. i.e, a new TOTP code is generated every time_step seconds. Must be between 20 and 60 seconds, inclusive. The default value is defined at the service level in the property `totp.time_step`. Defaults to 30 seconds if not configured.  Used when `factor_type` is `totp`
    47  	ConfigTimeStep *int `json:"Config.TimeStep,omitempty"`
    48  	// The number of time-steps, past and future, that are valid for validation of TOTP codes. Must be between 0 and 2, inclusive. The default value is defined at the service level in the property `totp.skew`. If not configured defaults to 1.  Used when `factor_type` is `totp`
    49  	ConfigSkew *int `json:"Config.Skew,omitempty"`
    50  	// Number of digits for generated TOTP codes. Must be between 3 and 8, inclusive. The default value is defined at the service level in the property `totp.code_length`. If not configured defaults to 6.  Used when `factor_type` is `totp`
    51  	ConfigCodeLength *int `json:"Config.CodeLength,omitempty"`
    52  	//
    53  	ConfigAlg *string `json:"Config.Alg,omitempty"`
    54  	// Custom metadata associated with the factor. This is added by the Device/SDK directly to allow for the inclusion of device information. It must be a stringified JSON with only strings values eg. `{\\\"os\\\": \\\"Android\\\"}`. Can be up to 1024 characters in length.
    55  	Metadata *interface{} `json:"Metadata,omitempty"`
    56  }
    57  
    58  func (params *CreateNewFactorParams) SetFriendlyName(FriendlyName string) *CreateNewFactorParams {
    59  	params.FriendlyName = &FriendlyName
    60  	return params
    61  }
    62  func (params *CreateNewFactorParams) SetFactorType(FactorType string) *CreateNewFactorParams {
    63  	params.FactorType = &FactorType
    64  	return params
    65  }
    66  func (params *CreateNewFactorParams) SetBindingAlg(BindingAlg string) *CreateNewFactorParams {
    67  	params.BindingAlg = &BindingAlg
    68  	return params
    69  }
    70  func (params *CreateNewFactorParams) SetBindingPublicKey(BindingPublicKey string) *CreateNewFactorParams {
    71  	params.BindingPublicKey = &BindingPublicKey
    72  	return params
    73  }
    74  func (params *CreateNewFactorParams) SetConfigAppId(ConfigAppId string) *CreateNewFactorParams {
    75  	params.ConfigAppId = &ConfigAppId
    76  	return params
    77  }
    78  func (params *CreateNewFactorParams) SetConfigNotificationPlatform(ConfigNotificationPlatform string) *CreateNewFactorParams {
    79  	params.ConfigNotificationPlatform = &ConfigNotificationPlatform
    80  	return params
    81  }
    82  func (params *CreateNewFactorParams) SetConfigNotificationToken(ConfigNotificationToken string) *CreateNewFactorParams {
    83  	params.ConfigNotificationToken = &ConfigNotificationToken
    84  	return params
    85  }
    86  func (params *CreateNewFactorParams) SetConfigSdkVersion(ConfigSdkVersion string) *CreateNewFactorParams {
    87  	params.ConfigSdkVersion = &ConfigSdkVersion
    88  	return params
    89  }
    90  func (params *CreateNewFactorParams) SetBindingSecret(BindingSecret string) *CreateNewFactorParams {
    91  	params.BindingSecret = &BindingSecret
    92  	return params
    93  }
    94  func (params *CreateNewFactorParams) SetConfigTimeStep(ConfigTimeStep int) *CreateNewFactorParams {
    95  	params.ConfigTimeStep = &ConfigTimeStep
    96  	return params
    97  }
    98  func (params *CreateNewFactorParams) SetConfigSkew(ConfigSkew int) *CreateNewFactorParams {
    99  	params.ConfigSkew = &ConfigSkew
   100  	return params
   101  }
   102  func (params *CreateNewFactorParams) SetConfigCodeLength(ConfigCodeLength int) *CreateNewFactorParams {
   103  	params.ConfigCodeLength = &ConfigCodeLength
   104  	return params
   105  }
   106  func (params *CreateNewFactorParams) SetConfigAlg(ConfigAlg string) *CreateNewFactorParams {
   107  	params.ConfigAlg = &ConfigAlg
   108  	return params
   109  }
   110  func (params *CreateNewFactorParams) SetMetadata(Metadata interface{}) *CreateNewFactorParams {
   111  	params.Metadata = &Metadata
   112  	return params
   113  }
   114  
   115  // Create a new Factor for the Entity
   116  func (c *ApiService) CreateNewFactor(ServiceSid string, Identity string, params *CreateNewFactorParams) (*VerifyV2NewFactor, error) {
   117  	path := "/v2/Services/{ServiceSid}/Entities/{Identity}/Factors"
   118  	path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1)
   119  	path = strings.Replace(path, "{"+"Identity"+"}", Identity, -1)
   120  
   121  	data := url.Values{}
   122  	headers := make(map[string]interface{})
   123  
   124  	if params != nil && params.FriendlyName != nil {
   125  		data.Set("FriendlyName", *params.FriendlyName)
   126  	}
   127  	if params != nil && params.FactorType != nil {
   128  		data.Set("FactorType", *params.FactorType)
   129  	}
   130  	if params != nil && params.BindingAlg != nil {
   131  		data.Set("Binding.Alg", *params.BindingAlg)
   132  	}
   133  	if params != nil && params.BindingPublicKey != nil {
   134  		data.Set("Binding.PublicKey", *params.BindingPublicKey)
   135  	}
   136  	if params != nil && params.ConfigAppId != nil {
   137  		data.Set("Config.AppId", *params.ConfigAppId)
   138  	}
   139  	if params != nil && params.ConfigNotificationPlatform != nil {
   140  		data.Set("Config.NotificationPlatform", *params.ConfigNotificationPlatform)
   141  	}
   142  	if params != nil && params.ConfigNotificationToken != nil {
   143  		data.Set("Config.NotificationToken", *params.ConfigNotificationToken)
   144  	}
   145  	if params != nil && params.ConfigSdkVersion != nil {
   146  		data.Set("Config.SdkVersion", *params.ConfigSdkVersion)
   147  	}
   148  	if params != nil && params.BindingSecret != nil {
   149  		data.Set("Binding.Secret", *params.BindingSecret)
   150  	}
   151  	if params != nil && params.ConfigTimeStep != nil {
   152  		data.Set("Config.TimeStep", fmt.Sprint(*params.ConfigTimeStep))
   153  	}
   154  	if params != nil && params.ConfigSkew != nil {
   155  		data.Set("Config.Skew", fmt.Sprint(*params.ConfigSkew))
   156  	}
   157  	if params != nil && params.ConfigCodeLength != nil {
   158  		data.Set("Config.CodeLength", fmt.Sprint(*params.ConfigCodeLength))
   159  	}
   160  	if params != nil && params.ConfigAlg != nil {
   161  		data.Set("Config.Alg", *params.ConfigAlg)
   162  	}
   163  	if params != nil && params.Metadata != nil {
   164  		v, err := json.Marshal(params.Metadata)
   165  
   166  		if err != nil {
   167  			return nil, err
   168  		}
   169  
   170  		data.Set("Metadata", string(v))
   171  	}
   172  
   173  	resp, err := c.requestHandler.Post(c.baseURL+path, data, headers)
   174  	if err != nil {
   175  		return nil, err
   176  	}
   177  
   178  	defer resp.Body.Close()
   179  
   180  	ps := &VerifyV2NewFactor{}
   181  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   182  		return nil, err
   183  	}
   184  
   185  	return ps, err
   186  }
   187  
   188  // Delete a specific Factor.
   189  func (c *ApiService) DeleteFactor(ServiceSid string, Identity string, Sid string) error {
   190  	path := "/v2/Services/{ServiceSid}/Entities/{Identity}/Factors/{Sid}"
   191  	path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1)
   192  	path = strings.Replace(path, "{"+"Identity"+"}", Identity, -1)
   193  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
   194  
   195  	data := url.Values{}
   196  	headers := make(map[string]interface{})
   197  
   198  	resp, err := c.requestHandler.Delete(c.baseURL+path, data, headers)
   199  	if err != nil {
   200  		return err
   201  	}
   202  
   203  	defer resp.Body.Close()
   204  
   205  	return nil
   206  }
   207  
   208  // Fetch a specific Factor.
   209  func (c *ApiService) FetchFactor(ServiceSid string, Identity string, Sid string) (*VerifyV2Factor, error) {
   210  	path := "/v2/Services/{ServiceSid}/Entities/{Identity}/Factors/{Sid}"
   211  	path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1)
   212  	path = strings.Replace(path, "{"+"Identity"+"}", Identity, -1)
   213  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
   214  
   215  	data := url.Values{}
   216  	headers := make(map[string]interface{})
   217  
   218  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
   219  	if err != nil {
   220  		return nil, err
   221  	}
   222  
   223  	defer resp.Body.Close()
   224  
   225  	ps := &VerifyV2Factor{}
   226  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   227  		return nil, err
   228  	}
   229  
   230  	return ps, err
   231  }
   232  
   233  // Optional parameters for the method 'ListFactor'
   234  type ListFactorParams struct {
   235  	// How many resources to return in each list page. The default is 50, and the maximum is 1000.
   236  	PageSize *int `json:"PageSize,omitempty"`
   237  	// Max number of records to return.
   238  	Limit *int `json:"limit,omitempty"`
   239  }
   240  
   241  func (params *ListFactorParams) SetPageSize(PageSize int) *ListFactorParams {
   242  	params.PageSize = &PageSize
   243  	return params
   244  }
   245  func (params *ListFactorParams) SetLimit(Limit int) *ListFactorParams {
   246  	params.Limit = &Limit
   247  	return params
   248  }
   249  
   250  // Retrieve a single page of Factor records from the API. Request is executed immediately.
   251  func (c *ApiService) PageFactor(ServiceSid string, Identity string, params *ListFactorParams, pageToken, pageNumber string) (*ListFactorResponse, error) {
   252  	path := "/v2/Services/{ServiceSid}/Entities/{Identity}/Factors"
   253  
   254  	path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1)
   255  	path = strings.Replace(path, "{"+"Identity"+"}", Identity, -1)
   256  
   257  	data := url.Values{}
   258  	headers := make(map[string]interface{})
   259  
   260  	if params != nil && params.PageSize != nil {
   261  		data.Set("PageSize", fmt.Sprint(*params.PageSize))
   262  	}
   263  
   264  	if pageToken != "" {
   265  		data.Set("PageToken", pageToken)
   266  	}
   267  	if pageNumber != "" {
   268  		data.Set("Page", pageNumber)
   269  	}
   270  
   271  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
   272  	if err != nil {
   273  		return nil, err
   274  	}
   275  
   276  	defer resp.Body.Close()
   277  
   278  	ps := &ListFactorResponse{}
   279  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   280  		return nil, err
   281  	}
   282  
   283  	return ps, err
   284  }
   285  
   286  // Lists Factor records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning.
   287  func (c *ApiService) ListFactor(ServiceSid string, Identity string, params *ListFactorParams) ([]VerifyV2Factor, error) {
   288  	response, errors := c.StreamFactor(ServiceSid, Identity, params)
   289  
   290  	records := make([]VerifyV2Factor, 0)
   291  	for record := range response {
   292  		records = append(records, record)
   293  	}
   294  
   295  	if err := <-errors; err != nil {
   296  		return nil, err
   297  	}
   298  
   299  	return records, nil
   300  }
   301  
   302  // Streams Factor records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached.
   303  func (c *ApiService) StreamFactor(ServiceSid string, Identity string, params *ListFactorParams) (chan VerifyV2Factor, chan error) {
   304  	if params == nil {
   305  		params = &ListFactorParams{}
   306  	}
   307  	params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit))
   308  
   309  	recordChannel := make(chan VerifyV2Factor, 1)
   310  	errorChannel := make(chan error, 1)
   311  
   312  	response, err := c.PageFactor(ServiceSid, Identity, params, "", "")
   313  	if err != nil {
   314  		errorChannel <- err
   315  		close(recordChannel)
   316  		close(errorChannel)
   317  	} else {
   318  		go c.streamFactor(response, params, recordChannel, errorChannel)
   319  	}
   320  
   321  	return recordChannel, errorChannel
   322  }
   323  
   324  func (c *ApiService) streamFactor(response *ListFactorResponse, params *ListFactorParams, recordChannel chan VerifyV2Factor, errorChannel chan error) {
   325  	curRecord := 1
   326  
   327  	for response != nil {
   328  		responseRecords := response.Factors
   329  		for item := range responseRecords {
   330  			recordChannel <- responseRecords[item]
   331  			curRecord += 1
   332  			if params.Limit != nil && *params.Limit < curRecord {
   333  				close(recordChannel)
   334  				close(errorChannel)
   335  				return
   336  			}
   337  		}
   338  
   339  		record, err := client.GetNext(c.baseURL, response, c.getNextListFactorResponse)
   340  		if err != nil {
   341  			errorChannel <- err
   342  			break
   343  		} else if record == nil {
   344  			break
   345  		}
   346  
   347  		response = record.(*ListFactorResponse)
   348  	}
   349  
   350  	close(recordChannel)
   351  	close(errorChannel)
   352  }
   353  
   354  func (c *ApiService) getNextListFactorResponse(nextPageUrl string) (interface{}, error) {
   355  	if nextPageUrl == "" {
   356  		return nil, nil
   357  	}
   358  	resp, err := c.requestHandler.Get(nextPageUrl, nil, nil)
   359  	if err != nil {
   360  		return nil, err
   361  	}
   362  
   363  	defer resp.Body.Close()
   364  
   365  	ps := &ListFactorResponse{}
   366  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   367  		return nil, err
   368  	}
   369  	return ps, nil
   370  }
   371  
   372  // Optional parameters for the method 'UpdateFactor'
   373  type UpdateFactorParams struct {
   374  	// The optional payload needed to verify the Factor for the first time. E.g. for a TOTP, the numeric code.
   375  	AuthPayload *string `json:"AuthPayload,omitempty"`
   376  	// The new friendly name of this Factor. It can be up to 64 characters.
   377  	FriendlyName *string `json:"FriendlyName,omitempty"`
   378  	// For APN, the device token. For FCM, the registration token. It is used to send the push notifications. Required when `factor_type` is `push`. If specified, this value must be between 32 and 255 characters long.
   379  	ConfigNotificationToken *string `json:"Config.NotificationToken,omitempty"`
   380  	// The Verify Push SDK version used to configure the factor
   381  	ConfigSdkVersion *string `json:"Config.SdkVersion,omitempty"`
   382  	// Defines how often, in seconds, are TOTP codes generated. i.e, a new TOTP code is generated every time_step seconds. Must be between 20 and 60 seconds, inclusive
   383  	ConfigTimeStep *int `json:"Config.TimeStep,omitempty"`
   384  	// The number of time-steps, past and future, that are valid for validation of TOTP codes. Must be between 0 and 2, inclusive
   385  	ConfigSkew *int `json:"Config.Skew,omitempty"`
   386  	// Number of digits for generated TOTP codes. Must be between 3 and 8, inclusive
   387  	ConfigCodeLength *int `json:"Config.CodeLength,omitempty"`
   388  	//
   389  	ConfigAlg *string `json:"Config.Alg,omitempty"`
   390  	// The transport technology used to generate the Notification Token. Can be `apn`, `fcm` or `none`.  Required when `factor_type` is `push`.
   391  	ConfigNotificationPlatform *string `json:"Config.NotificationPlatform,omitempty"`
   392  }
   393  
   394  func (params *UpdateFactorParams) SetAuthPayload(AuthPayload string) *UpdateFactorParams {
   395  	params.AuthPayload = &AuthPayload
   396  	return params
   397  }
   398  func (params *UpdateFactorParams) SetFriendlyName(FriendlyName string) *UpdateFactorParams {
   399  	params.FriendlyName = &FriendlyName
   400  	return params
   401  }
   402  func (params *UpdateFactorParams) SetConfigNotificationToken(ConfigNotificationToken string) *UpdateFactorParams {
   403  	params.ConfigNotificationToken = &ConfigNotificationToken
   404  	return params
   405  }
   406  func (params *UpdateFactorParams) SetConfigSdkVersion(ConfigSdkVersion string) *UpdateFactorParams {
   407  	params.ConfigSdkVersion = &ConfigSdkVersion
   408  	return params
   409  }
   410  func (params *UpdateFactorParams) SetConfigTimeStep(ConfigTimeStep int) *UpdateFactorParams {
   411  	params.ConfigTimeStep = &ConfigTimeStep
   412  	return params
   413  }
   414  func (params *UpdateFactorParams) SetConfigSkew(ConfigSkew int) *UpdateFactorParams {
   415  	params.ConfigSkew = &ConfigSkew
   416  	return params
   417  }
   418  func (params *UpdateFactorParams) SetConfigCodeLength(ConfigCodeLength int) *UpdateFactorParams {
   419  	params.ConfigCodeLength = &ConfigCodeLength
   420  	return params
   421  }
   422  func (params *UpdateFactorParams) SetConfigAlg(ConfigAlg string) *UpdateFactorParams {
   423  	params.ConfigAlg = &ConfigAlg
   424  	return params
   425  }
   426  func (params *UpdateFactorParams) SetConfigNotificationPlatform(ConfigNotificationPlatform string) *UpdateFactorParams {
   427  	params.ConfigNotificationPlatform = &ConfigNotificationPlatform
   428  	return params
   429  }
   430  
   431  // Update a specific Factor. This endpoint can be used to Verify a Factor if passed an `AuthPayload` param.
   432  func (c *ApiService) UpdateFactor(ServiceSid string, Identity string, Sid string, params *UpdateFactorParams) (*VerifyV2Factor, error) {
   433  	path := "/v2/Services/{ServiceSid}/Entities/{Identity}/Factors/{Sid}"
   434  	path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1)
   435  	path = strings.Replace(path, "{"+"Identity"+"}", Identity, -1)
   436  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
   437  
   438  	data := url.Values{}
   439  	headers := make(map[string]interface{})
   440  
   441  	if params != nil && params.AuthPayload != nil {
   442  		data.Set("AuthPayload", *params.AuthPayload)
   443  	}
   444  	if params != nil && params.FriendlyName != nil {
   445  		data.Set("FriendlyName", *params.FriendlyName)
   446  	}
   447  	if params != nil && params.ConfigNotificationToken != nil {
   448  		data.Set("Config.NotificationToken", *params.ConfigNotificationToken)
   449  	}
   450  	if params != nil && params.ConfigSdkVersion != nil {
   451  		data.Set("Config.SdkVersion", *params.ConfigSdkVersion)
   452  	}
   453  	if params != nil && params.ConfigTimeStep != nil {
   454  		data.Set("Config.TimeStep", fmt.Sprint(*params.ConfigTimeStep))
   455  	}
   456  	if params != nil && params.ConfigSkew != nil {
   457  		data.Set("Config.Skew", fmt.Sprint(*params.ConfigSkew))
   458  	}
   459  	if params != nil && params.ConfigCodeLength != nil {
   460  		data.Set("Config.CodeLength", fmt.Sprint(*params.ConfigCodeLength))
   461  	}
   462  	if params != nil && params.ConfigAlg != nil {
   463  		data.Set("Config.Alg", *params.ConfigAlg)
   464  	}
   465  	if params != nil && params.ConfigNotificationPlatform != nil {
   466  		data.Set("Config.NotificationPlatform", *params.ConfigNotificationPlatform)
   467  	}
   468  
   469  	resp, err := c.requestHandler.Post(c.baseURL+path, data, headers)
   470  	if err != nil {
   471  		return nil, err
   472  	}
   473  
   474  	defer resp.Body.Close()
   475  
   476  	ps := &VerifyV2Factor{}
   477  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   478  		return nil, err
   479  	}
   480  
   481  	return ps, err
   482  }