-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
70 lines (55 loc) · 2.13 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package main
import (
"bytes"
"encoding/base64"
"flag"
"fmt"
"hotpTest/crypto"
"io/ioutil"
"net/http"
"os"
)
// A simple program to test a hotp based api
// TODO: Add functionality to set values directly from enviroment variables
func main() {
var passLength int
var t0, interval int64
var hashAlgorithm, secretKey, targetApi, userid, message, passLenFormat string
// TODO: Improve parameter parsing
// Begin parsing
flag.StringVar(&secretKey, "secret", "defaultPass", "The secret used with the hmac algorithm.")
flag.StringVar(&hashAlgorithm, "hash", "sha512", "Type of hash algorithm used, default is sha512.")
flag.StringVar(&targetApi, "target", "", "The site to post the message using hotp.")
flag.StringVar(&userid, "userid", "default@gmail.com", "Your user id.")
flag.StringVar(&message, "message", "{userid: abc, lol: \"sample message.\"}", "Message to send.")
flag.Int64Var(&t0, "initial", 0, "t0")
flag.Int64Var(&interval, "interval", 30, "Interval between new hotp")
flag.IntVar(&passLength, "length", 10, "Length of the hotp.")
flag.Parse()
// End parsing
// Format for padding with leading zeroes if password length is less than passLength
passLenFormat = fmt.Sprintf("%%0%dd", passLength)
password := hotp.CalcHotp(hashAlgorithm, []byte(secretKey), t0, interval, passLength)
auth := base64.StdEncoding.EncodeToString([]byte(userid + ":" + fmt.Sprintf(passLenFormat, password)))
fmt.Printf("%s\n", fmt.Sprintf(passLenFormat, password))
fmt.Printf("auth: %s\n", auth)
if targetApi == "" {
fmt.Println("Skipping Api Test")
os.Exit(0)
}
fmt.Printf("Initiating api test. Sending http request to %s\n", targetApi)
req, _ := http.NewRequest("POST", targetApi, bytes.NewBuffer([]byte(message)))
req.Header.Set("Content-Type", "application/json")
req.Header.Set("Authorization", "Basic "+string(auth))
req.Header.Set("Accept", "*/*")
client := &http.Client{}
response, err := client.Do(req)
if err != nil {
panic(err)
}
defer response.Body.Close()
fmt.Println("Status: ", response.Status)
fmt.Println("Headers: ", response.Header)
body, _ := ioutil.ReadAll(response.Body)
fmt.Println("Body: ", string(body))
}