Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions otp.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ package ipa

import (
"encoding/json"
"fmt"
"net/http"
"net/url"
"strings"
)

Expand Down Expand Up @@ -198,3 +201,35 @@ func (c *Client) DisableOTPToken(tokenID string) error {

return err
}

// SyncTOTPToken Returns error
func (c *Client) SyncTOTPToken(uid, passwd, firstCode, secondCode string) error {
ipaURL := fmt.Sprintf("https://%s/ipa/session/sync_token", c.host)
form := url.Values{
"user": {uid},
"password": {passwd},
"first_code": {firstCode},
"second_code": {secondCode},
}
req, err := http.NewRequest("POST", ipaURL, strings.NewReader(form.Encode()))
if err != nil {
return err
}
req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
req.Header.Set("Referer", fmt.Sprintf("https://%s/ipa", c.host))

res, err := c.httpClient.Do(req)
if err != nil {
return err
}
defer res.Body.Close()
if res.StatusCode != 200 {
return fmt.Errorf("IPA sync token failed with HTTP status code: %d", res.StatusCode)
}

if res.Header.Get("X-Ipa-Tokensync-Result") == "invalid-credentials" {
return fmt.Errorf("User/Pass/Token is invalid")
}

return nil
}