github.com/twilio/twilio-go@v1.20.1/rest/verify/v2/services_entities_challenges.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  	"time"
    23  
    24  	"github.com/twilio/twilio-go/client"
    25  )
    26  
    27  // Optional parameters for the method 'CreateChallenge'
    28  type CreateChallengeParams struct {
    29  	// The unique SID identifier of the Factor.
    30  	FactorSid *string `json:"FactorSid,omitempty"`
    31  	// The date-time when this Challenge expires, given in [ISO 8601](https://en.wikipedia.org/wiki/ISO_8601) format. The default value is five (5) minutes after Challenge creation. The max value is sixty (60) minutes after creation.
    32  	ExpirationDate *time.Time `json:"ExpirationDate,omitempty"`
    33  	// Shown to the user when the push notification arrives. Required when `factor_type` is `push`. Can be up to 256 characters in length
    34  	DetailsMessage *string `json:"Details.Message,omitempty"`
    35  	// A list of objects that describe the Fields included in the Challenge. Each object contains the label and value of the field, the label can be up to 36 characters in length and the value can be up to 128 characters in length. Used when `factor_type` is `push`. There can be up to 20 details fields.
    36  	DetailsFields *[]interface{} `json:"Details.Fields,omitempty"`
    37  	// Details provided to give context about the Challenge. Not shown to the end user. It must be a stringified JSON with only strings values eg. `{\\\"ip\\\": \\\"172.168.1.234\\\"}`. Can be up to 1024 characters in length
    38  	HiddenDetails *interface{} `json:"HiddenDetails,omitempty"`
    39  	// Optional payload used to verify the Challenge upon creation. Only used with a Factor of type `totp` to carry the TOTP code that needs to be verified. For `TOTP` this value must be between 3 and 8 characters long.
    40  	AuthPayload *string `json:"AuthPayload,omitempty"`
    41  }
    42  
    43  func (params *CreateChallengeParams) SetFactorSid(FactorSid string) *CreateChallengeParams {
    44  	params.FactorSid = &FactorSid
    45  	return params
    46  }
    47  func (params *CreateChallengeParams) SetExpirationDate(ExpirationDate time.Time) *CreateChallengeParams {
    48  	params.ExpirationDate = &ExpirationDate
    49  	return params
    50  }
    51  func (params *CreateChallengeParams) SetDetailsMessage(DetailsMessage string) *CreateChallengeParams {
    52  	params.DetailsMessage = &DetailsMessage
    53  	return params
    54  }
    55  func (params *CreateChallengeParams) SetDetailsFields(DetailsFields []interface{}) *CreateChallengeParams {
    56  	params.DetailsFields = &DetailsFields
    57  	return params
    58  }
    59  func (params *CreateChallengeParams) SetHiddenDetails(HiddenDetails interface{}) *CreateChallengeParams {
    60  	params.HiddenDetails = &HiddenDetails
    61  	return params
    62  }
    63  func (params *CreateChallengeParams) SetAuthPayload(AuthPayload string) *CreateChallengeParams {
    64  	params.AuthPayload = &AuthPayload
    65  	return params
    66  }
    67  
    68  // Create a new Challenge for the Factor
    69  func (c *ApiService) CreateChallenge(ServiceSid string, Identity string, params *CreateChallengeParams) (*VerifyV2Challenge, error) {
    70  	path := "/v2/Services/{ServiceSid}/Entities/{Identity}/Challenges"
    71  	path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1)
    72  	path = strings.Replace(path, "{"+"Identity"+"}", Identity, -1)
    73  
    74  	data := url.Values{}
    75  	headers := make(map[string]interface{})
    76  
    77  	if params != nil && params.FactorSid != nil {
    78  		data.Set("FactorSid", *params.FactorSid)
    79  	}
    80  	if params != nil && params.ExpirationDate != nil {
    81  		data.Set("ExpirationDate", fmt.Sprint((*params.ExpirationDate).Format(time.RFC3339)))
    82  	}
    83  	if params != nil && params.DetailsMessage != nil {
    84  		data.Set("Details.Message", *params.DetailsMessage)
    85  	}
    86  	if params != nil && params.DetailsFields != nil {
    87  		for _, item := range *params.DetailsFields {
    88  			v, err := json.Marshal(item)
    89  
    90  			if err != nil {
    91  				return nil, err
    92  			}
    93  
    94  			data.Add("Details.Fields", string(v))
    95  		}
    96  	}
    97  	if params != nil && params.HiddenDetails != nil {
    98  		v, err := json.Marshal(params.HiddenDetails)
    99  
   100  		if err != nil {
   101  			return nil, err
   102  		}
   103  
   104  		data.Set("HiddenDetails", string(v))
   105  	}
   106  	if params != nil && params.AuthPayload != nil {
   107  		data.Set("AuthPayload", *params.AuthPayload)
   108  	}
   109  
   110  	resp, err := c.requestHandler.Post(c.baseURL+path, data, headers)
   111  	if err != nil {
   112  		return nil, err
   113  	}
   114  
   115  	defer resp.Body.Close()
   116  
   117  	ps := &VerifyV2Challenge{}
   118  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   119  		return nil, err
   120  	}
   121  
   122  	return ps, err
   123  }
   124  
   125  // Fetch a specific Challenge.
   126  func (c *ApiService) FetchChallenge(ServiceSid string, Identity string, Sid string) (*VerifyV2Challenge, error) {
   127  	path := "/v2/Services/{ServiceSid}/Entities/{Identity}/Challenges/{Sid}"
   128  	path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1)
   129  	path = strings.Replace(path, "{"+"Identity"+"}", Identity, -1)
   130  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
   131  
   132  	data := url.Values{}
   133  	headers := make(map[string]interface{})
   134  
   135  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
   136  	if err != nil {
   137  		return nil, err
   138  	}
   139  
   140  	defer resp.Body.Close()
   141  
   142  	ps := &VerifyV2Challenge{}
   143  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   144  		return nil, err
   145  	}
   146  
   147  	return ps, err
   148  }
   149  
   150  // Optional parameters for the method 'ListChallenge'
   151  type ListChallengeParams struct {
   152  	// The unique SID identifier of the Factor.
   153  	FactorSid *string `json:"FactorSid,omitempty"`
   154  	// The Status of the Challenges to fetch. One of `pending`, `expired`, `approved` or `denied`.
   155  	Status *string `json:"Status,omitempty"`
   156  	// The desired sort order of the Challenges list. One of `asc` or `desc` for ascending and descending respectively. Defaults to `asc`.
   157  	Order *string `json:"Order,omitempty"`
   158  	// How many resources to return in each list page. The default is 50, and the maximum is 1000.
   159  	PageSize *int `json:"PageSize,omitempty"`
   160  	// Max number of records to return.
   161  	Limit *int `json:"limit,omitempty"`
   162  }
   163  
   164  func (params *ListChallengeParams) SetFactorSid(FactorSid string) *ListChallengeParams {
   165  	params.FactorSid = &FactorSid
   166  	return params
   167  }
   168  func (params *ListChallengeParams) SetStatus(Status string) *ListChallengeParams {
   169  	params.Status = &Status
   170  	return params
   171  }
   172  func (params *ListChallengeParams) SetOrder(Order string) *ListChallengeParams {
   173  	params.Order = &Order
   174  	return params
   175  }
   176  func (params *ListChallengeParams) SetPageSize(PageSize int) *ListChallengeParams {
   177  	params.PageSize = &PageSize
   178  	return params
   179  }
   180  func (params *ListChallengeParams) SetLimit(Limit int) *ListChallengeParams {
   181  	params.Limit = &Limit
   182  	return params
   183  }
   184  
   185  // Retrieve a single page of Challenge records from the API. Request is executed immediately.
   186  func (c *ApiService) PageChallenge(ServiceSid string, Identity string, params *ListChallengeParams, pageToken, pageNumber string) (*ListChallengeResponse, error) {
   187  	path := "/v2/Services/{ServiceSid}/Entities/{Identity}/Challenges"
   188  
   189  	path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1)
   190  	path = strings.Replace(path, "{"+"Identity"+"}", Identity, -1)
   191  
   192  	data := url.Values{}
   193  	headers := make(map[string]interface{})
   194  
   195  	if params != nil && params.FactorSid != nil {
   196  		data.Set("FactorSid", *params.FactorSid)
   197  	}
   198  	if params != nil && params.Status != nil {
   199  		data.Set("Status", *params.Status)
   200  	}
   201  	if params != nil && params.Order != nil {
   202  		data.Set("Order", *params.Order)
   203  	}
   204  	if params != nil && params.PageSize != nil {
   205  		data.Set("PageSize", fmt.Sprint(*params.PageSize))
   206  	}
   207  
   208  	if pageToken != "" {
   209  		data.Set("PageToken", pageToken)
   210  	}
   211  	if pageNumber != "" {
   212  		data.Set("Page", pageNumber)
   213  	}
   214  
   215  	resp, err := c.requestHandler.Get(c.baseURL+path, data, headers)
   216  	if err != nil {
   217  		return nil, err
   218  	}
   219  
   220  	defer resp.Body.Close()
   221  
   222  	ps := &ListChallengeResponse{}
   223  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   224  		return nil, err
   225  	}
   226  
   227  	return ps, err
   228  }
   229  
   230  // Lists Challenge records from the API as a list. Unlike stream, this operation is eager and loads 'limit' records into memory before returning.
   231  func (c *ApiService) ListChallenge(ServiceSid string, Identity string, params *ListChallengeParams) ([]VerifyV2Challenge, error) {
   232  	response, errors := c.StreamChallenge(ServiceSid, Identity, params)
   233  
   234  	records := make([]VerifyV2Challenge, 0)
   235  	for record := range response {
   236  		records = append(records, record)
   237  	}
   238  
   239  	if err := <-errors; err != nil {
   240  		return nil, err
   241  	}
   242  
   243  	return records, nil
   244  }
   245  
   246  // Streams Challenge records from the API as a channel stream. This operation lazily loads records as efficiently as possible until the limit is reached.
   247  func (c *ApiService) StreamChallenge(ServiceSid string, Identity string, params *ListChallengeParams) (chan VerifyV2Challenge, chan error) {
   248  	if params == nil {
   249  		params = &ListChallengeParams{}
   250  	}
   251  	params.SetPageSize(client.ReadLimits(params.PageSize, params.Limit))
   252  
   253  	recordChannel := make(chan VerifyV2Challenge, 1)
   254  	errorChannel := make(chan error, 1)
   255  
   256  	response, err := c.PageChallenge(ServiceSid, Identity, params, "", "")
   257  	if err != nil {
   258  		errorChannel <- err
   259  		close(recordChannel)
   260  		close(errorChannel)
   261  	} else {
   262  		go c.streamChallenge(response, params, recordChannel, errorChannel)
   263  	}
   264  
   265  	return recordChannel, errorChannel
   266  }
   267  
   268  func (c *ApiService) streamChallenge(response *ListChallengeResponse, params *ListChallengeParams, recordChannel chan VerifyV2Challenge, errorChannel chan error) {
   269  	curRecord := 1
   270  
   271  	for response != nil {
   272  		responseRecords := response.Challenges
   273  		for item := range responseRecords {
   274  			recordChannel <- responseRecords[item]
   275  			curRecord += 1
   276  			if params.Limit != nil && *params.Limit < curRecord {
   277  				close(recordChannel)
   278  				close(errorChannel)
   279  				return
   280  			}
   281  		}
   282  
   283  		record, err := client.GetNext(c.baseURL, response, c.getNextListChallengeResponse)
   284  		if err != nil {
   285  			errorChannel <- err
   286  			break
   287  		} else if record == nil {
   288  			break
   289  		}
   290  
   291  		response = record.(*ListChallengeResponse)
   292  	}
   293  
   294  	close(recordChannel)
   295  	close(errorChannel)
   296  }
   297  
   298  func (c *ApiService) getNextListChallengeResponse(nextPageUrl string) (interface{}, error) {
   299  	if nextPageUrl == "" {
   300  		return nil, nil
   301  	}
   302  	resp, err := c.requestHandler.Get(nextPageUrl, nil, nil)
   303  	if err != nil {
   304  		return nil, err
   305  	}
   306  
   307  	defer resp.Body.Close()
   308  
   309  	ps := &ListChallengeResponse{}
   310  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   311  		return nil, err
   312  	}
   313  	return ps, nil
   314  }
   315  
   316  // Optional parameters for the method 'UpdateChallenge'
   317  type UpdateChallengeParams struct {
   318  	// The optional payload needed to verify the Challenge. E.g., a TOTP would use the numeric code. For `TOTP` this value must be between 3 and 8 characters long. For `Push` this value can be up to 5456 characters in length
   319  	AuthPayload *string `json:"AuthPayload,omitempty"`
   320  	// Custom metadata associated with the challenge. 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.
   321  	Metadata *interface{} `json:"Metadata,omitempty"`
   322  }
   323  
   324  func (params *UpdateChallengeParams) SetAuthPayload(AuthPayload string) *UpdateChallengeParams {
   325  	params.AuthPayload = &AuthPayload
   326  	return params
   327  }
   328  func (params *UpdateChallengeParams) SetMetadata(Metadata interface{}) *UpdateChallengeParams {
   329  	params.Metadata = &Metadata
   330  	return params
   331  }
   332  
   333  // Verify a specific Challenge.
   334  func (c *ApiService) UpdateChallenge(ServiceSid string, Identity string, Sid string, params *UpdateChallengeParams) (*VerifyV2Challenge, error) {
   335  	path := "/v2/Services/{ServiceSid}/Entities/{Identity}/Challenges/{Sid}"
   336  	path = strings.Replace(path, "{"+"ServiceSid"+"}", ServiceSid, -1)
   337  	path = strings.Replace(path, "{"+"Identity"+"}", Identity, -1)
   338  	path = strings.Replace(path, "{"+"Sid"+"}", Sid, -1)
   339  
   340  	data := url.Values{}
   341  	headers := make(map[string]interface{})
   342  
   343  	if params != nil && params.AuthPayload != nil {
   344  		data.Set("AuthPayload", *params.AuthPayload)
   345  	}
   346  	if params != nil && params.Metadata != nil {
   347  		v, err := json.Marshal(params.Metadata)
   348  
   349  		if err != nil {
   350  			return nil, err
   351  		}
   352  
   353  		data.Set("Metadata", string(v))
   354  	}
   355  
   356  	resp, err := c.requestHandler.Post(c.baseURL+path, data, headers)
   357  	if err != nil {
   358  		return nil, err
   359  	}
   360  
   361  	defer resp.Body.Close()
   362  
   363  	ps := &VerifyV2Challenge{}
   364  	if err := json.NewDecoder(resp.Body).Decode(ps); err != nil {
   365  		return nil, err
   366  	}
   367  
   368  	return ps, err
   369  }