forked from guilhermebr/go-stone-openbank
-
Notifications
You must be signed in to change notification settings - Fork 0
/
authentication.go
140 lines (114 loc) · 3.08 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package openbank
import (
"context"
"encoding/base64"
"encoding/json"
"fmt"
"net/http"
"net/url"
"strings"
"time"
"go.opentelemetry.io/otel/codes"
"go.opentelemetry.io/otel/trace"
jwt "github.com/golang-jwt/jwt/v4"
"github.com/google/uuid"
"golang.org/x/oauth2"
)
func (c *Client) Authenticate(sCtx context.Context) error {
ctx, span := c.newSpan(sCtx, "openbank auth", trace.SpanKindClient)
defer c.endSpan(span)
if c.validToken() {
c.setSpanStatus(span, codes.Ok, "authentication succeeded")
return nil
}
claims := c.authClaims()
tokenString, err := c.generateToken(claims)
if err != nil {
c.setSpanStatus(span, codes.Error, "error generating token")
c.spanRecordError(span, err)
return err
}
data := url.Values{}
data.Set("client_assertion_type", "urn:ietf:params:oauth:client-assertion-type:jwt-bearer")
data.Set("client_assertion", tokenString)
data.Set("client_id", c.ClientID)
data.Set("grant_type", "client_credentials")
u, err := c.AccountURL.Parse("/auth/realms/stone_bank/protocol/openid-connect/token")
if err != nil {
c.setSpanStatus(span, codes.Error, "error parsing URL")
c.spanRecordError(span, err)
return err
}
req, err := http.NewRequest("POST", u.String(), strings.NewReader(data.Encode()))
if err != nil {
c.setSpanStatus(span, codes.Error, "error creating request")
c.spanRecordError(span, err)
return err
}
req.Header.Add("user-agent", c.UserAgent)
req.Header.Add("content-type", "application/x-www-form-urlencoded")
req = req.WithContext(ctx)
var token oauth2.Token
_, err = c.Do(req, &token, new(TransferError))
if err != nil {
c.setSpanStatus(span, codes.Error, "error executing request")
c.spanRecordError(span, err)
return err
}
config := &oauth2.Config{}
ts := config.TokenSource(ctx, &token)
c.m.Lock()
defer c.m.Unlock()
c.client = oauth2.NewClient(ctx, ts)
c.token = token
c.setSpanStatus(span, codes.Ok, "authentication succeeded")
return nil
}
func (c *Client) authClaims() jwt.MapClaims {
now := time.Now()
u, _ := c.AccountURL.Parse("/auth/realms/stone_bank")
claims := jwt.MapClaims{
"aud": u.String(),
"client_id": c.ClientID,
"exp": now.Add(time.Hour * time.Duration(2)).Unix(),
"iat": now.Unix(),
"jti": uuid.New().String(),
"iss": c.ClientID,
"nbf": now.Unix(),
"realm": "stone_bank",
"sub": c.ClientID,
}
return claims
}
func (c *Client) validToken() bool {
if !c.token.Valid() {
return false
}
src := strings.Split(c.token.AccessToken, ".")
if len(src) != 3 {
return false
}
if l := len(src[1]) % 4; l > 0 {
src[1] += strings.Repeat("=", 4-l)
}
decoded, err := base64.URLEncoding.DecodeString(src[1])
if err != nil {
c.log.Error(fmt.Errorf("decoding base64 error %s", err))
return false
}
var output tokenData
err = json.Unmarshal(decoded, &output)
if err != nil {
c.log.Error(fmt.Errorf("decoding json error %s", err))
return false
}
tm := time.Unix(int64(output.Exp), 0)
remainder := tm.Sub(time.Now())
if remainder < 30 {
return false
}
return true
}
type tokenData struct {
Exp int `json:"exp"`
}