-
Notifications
You must be signed in to change notification settings - Fork 0
/
authentication.go
96 lines (88 loc) · 2.43 KB
/
authentication.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package daraja_wrapper
import (
"crypto/rand"
"crypto/rsa"
"crypto/x509"
b64 "encoding/base64"
"encoding/json"
"encoding/pem"
"fmt"
"io/ioutil"
"net/http"
"strings"
)
const Authentication_url = "https://sandbox.safaricom.co.ke/oauth/v1/generate?grant_type=client_credentials"
// Token - This is the token received from the
// daraja API. The `Token` struct is used to validate
// various actions done on the API
type Token struct {
Token string `json:"access_token"`
Expiry string `json:"expires_in"`
}
func (t Token) String() string {
return fmt.Sprintf("Token: '%s', Expires in(seconds): '%s'", t.Token, t.Expiry)
}
type Auth struct {
Key, Secret string
Prod bool
}
func (a Auth) String() string {
return fmt.Sprintf("Key: '%s', Secret: '%s', Prod: '%v'", a.Key, a.Secret, a.Prod)
}
func (a *Auth) GetAuthKey() (Token, error) {
if a.Secret == "" || a.Key == "" {
no_args := fmt.Errorf("The `Auth` struct is missing the `key` or `secret` field! \n Auth: '%v'", a)
return Token{}, no_args
}
client := &http.Client{}
url := a.setUrl(Authentication_url)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return Token{}, err
}
req.SetBasicAuth(a.Key, a.Secret)
req.Header.Add("Accept", "application/json")
if err != nil {
return Token{}, err
}
resp, err := client.Do(req)
if err != nil {
return Token{}, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return Token{}, err
}
var token Token
err = json.Unmarshal(body, &token)
if err != nil {
return token, err
}
return token, nil
}
func (a *Auth) setUrl(url string) string {
if a.Prod {
return strings.Replace(url, "sandbox.", "", 1)
} else {
return url
}
}
// `SecurityCredentials` - This function is for generating the `SecurityCredentials`
// used in the parameters for mpesa API
// `password` is the 'Initiator Security Password'
// `cert_file` is the contents of the cetificate(Leaving the getting of the file to the user)
func SecurityCredentials(password, cert_file []byte) (string, error) {
block, _ := pem.Decode(cert_file)
if block == nil {
return "", fmt.Errorf("There is no PEM data found!")
}
cert, err := x509.ParseCertificate(block.Bytes)
if err != nil {
return "", err
}
public := cert.PublicKey.(*rsa.PublicKey)
security_cred, err := rsa.EncryptPKCS1v15(rand.Reader, public, password)
security_cred_b64 := b64.StdEncoding.EncodeToString([]byte(security_cred))
return security_cred_b64, nil
}