forked from free5gc/openapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
oauth.go
302 lines (267 loc) · 7.35 KB
/
oauth.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
package openapi
import (
"bytes"
"context"
"crypto/rand"
"crypto/rsa"
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
"fmt"
"math/big"
"net/url"
"os"
"strconv"
"strings"
"time"
"github.com/golang-jwt/jwt"
"github.com/pkg/errors"
"github.com/free5gc/openapi/models"
)
type CCAClaims struct {
Iat int32
Exp int32
jwt.StandardClaims
}
func GenerateClientCredentialAssertion(
sub, aud, keyPath string,
) (string, error) {
var expiration int32 = 1000
now := int32(time.Now().Unix())
accessTokenClaims := CCAClaims{
Iat: now,
Exp: now + expiration, // access_token is authorized for use
StandardClaims: jwt.StandardClaims{
Subject: sub,
Audience: aud,
},
}
// Use RSA as a signing method
signKey, err := ParsePrivateKeyFromPEM(keyPath)
if err != nil {
return "", errors.Wrapf(err, "gen CCAClaims")
}
token := jwt.NewWithClaims(jwt.GetSigningMethod("RS512"), accessTokenClaims)
accessToken, err := token.SignedString(signKey)
if err != nil {
return "", errors.Wrapf(err, "gen CCAClaims")
}
return accessToken, nil
}
func VerifyOAuth(
authorization, serviceName, certPath string,
) error {
verifyKey, err := ParsePublicKeyFromPEM(certPath)
if err != nil {
return errors.Wrapf(err, "verify OAuth")
}
auth_fields := strings.Fields(authorization)
if len(auth_fields) < 2 {
return errors.Errorf("verify OAuth Authorization header invalid")
}
access_token := auth_fields[1]
token, err := jwt.ParseWithClaims(
access_token,
&models.AccessTokenClaims{},
func(token *jwt.Token) (interface{}, error) {
if _, ok := token.Method.(*jwt.SigningMethodRSA); !ok {
return nil, errors.Wrapf(err, "Unexpected signing method")
}
if token.Header["alg"] != "RS512" {
return nil, errors.Wrapf(err, "Unexpected signing method")
}
return verifyKey, nil
})
if err != nil {
return errors.Wrapf(err, "verify OAuth parse")
}
if !verifyScope(token.Claims.(*models.AccessTokenClaims).Scope, serviceName) {
return errors.Wrapf(err, "verify OAuth scope")
}
return nil
}
func verifyScope(scope, serviceName string) bool {
if len(serviceName) == 0 {
return true
}
if len(scope) != 0 {
scopeSplit := strings.Fields(scope)
found := false
for _, item := range scopeSplit {
if item == serviceName {
found = true
break
}
}
if !found {
return false
}
} else {
return false
}
return true
}
func GenerateRootCertificate(
rootCertPath string,
rootPrivKey *rsa.PrivateKey,
) (*x509.Certificate, error) {
rootCert, err := GenerateCertificate(
"", "", rootCertPath, &rootPrivKey.PublicKey, nil, rootPrivKey)
if err != nil {
return nil, errors.Wrapf(err, "gen root cert")
}
return rootCert, nil
}
func GenerateCertificate(
nfType, nfId, certPemPath string,
pubKey *rsa.PublicKey,
rootCert *x509.Certificate,
rootPrivKey *rsa.PrivateKey,
) (*x509.Certificate, error) {
max := new(big.Int)
max.Exp(big.NewInt(16), big.NewInt(40), nil)
sn, _ := rand.Int(rand.Reader, max)
temp := &x509.Certificate{
SerialNumber: sn,
Subject: pkix.Name{
Country: []string{"TW"},
Province: []string{"Taiwan"},
Locality: []string{"Hsinchu"},
Organization: []string{"free5gc"},
OrganizationalUnit: []string{"free5gc"},
},
NotBefore: time.Now(),
NotAfter: time.Now().AddDate(10, 0, 0),
}
if nfType != "" {
temp.Subject.CommonName = strings.ToUpper(nfType)
temp.DNSNames = []string{nfType}
}
if nfId != "" {
uri, err := url.Parse("urn:uuid:" + nfId)
if err != nil {
return nil, errors.Wrapf(err, "gen cert url")
}
temp.URIs = []*url.URL{uri}
}
if rootCert == nil {
// generate self-signed certificate
rootCert = temp
}
b, err := x509.CreateCertificate(
rand.Reader, temp, rootCert, pubKey, rootPrivKey)
if err != nil {
return nil, errors.Wrapf(err, "gen cert create")
}
cert, err := x509.ParseCertificate(b)
if err != nil {
return nil, errors.Wrapf(err, "gen cert parse")
}
if certPemPath != "" {
out := &bytes.Buffer{}
err = pem.Encode(out, &pem.Block{Type: "CERTIFICATE", Bytes: b})
if err != nil {
return nil, errors.Wrapf(err, "gen cert file encode")
}
certFile, err := os.Create(certPemPath)
if err != nil {
return nil, errors.Wrapf(err, "gen cert file create")
}
defer certFile.Close() // nolint
_, err = out.WriteTo(certFile)
if err != nil {
return nil, errors.Wrapf(err, "gen cert file write")
}
}
return cert, nil
}
func ParsePublicKeyFromPEM(pubPemPath string) (*rsa.PublicKey, error) {
b, err := os.ReadFile(pubPemPath)
if err != nil {
return nil, errors.Wrapf(err, "pubkey read")
}
pubKey, err := jwt.ParseRSAPublicKeyFromPEM(b)
if err != nil {
return nil, errors.Wrapf(err, "pubkey parse")
}
return pubKey, nil
}
func VerifyAccessTokenAndScopes(access_token string, key *rsa.PublicKey, scopes []string) (bool, error) {
if claims, err := VerifyAccessToken(access_token, key); err != nil {
return false, err
} else {
return verifyScopes(claims, scopes)
}
}
func verifyScopes(claims jwt.MapClaims, scopes []string) (bool, error) {
if len(scopes) == 0 {
return true, nil
} else if claims != nil {
if err := claims.Valid(); err != nil {
return false, fmt.Errorf("Claims invalid %+v", err.Error())
}
for _, scope := range scopes {
if claimScope, ok := claims["scope"]; !ok {
return false, fmt.Errorf("JWT Claims did not contain a scope.")
} else {
claimScopesSplit := strings.Fields(claimScope.(string))
found := false
for _, item := range claimScopesSplit {
if item == scope {
found = true
break
}
}
if !found {
return false, fmt.Errorf("Did not find scope in claims")
}
}
}
return true, nil
} else {
return false, fmt.Errorf("Claims was nil")
}
}
func VerifyAccessToken(access_token string, key *rsa.PublicKey) (claims jwt.MapClaims, err error) {
token, err := jwt.Parse(access_token, func(token *jwt.Token) (interface{}, error) {
// Don't forget to validate the alg is what you expect:
if _, ok := token.Method.(*jwt.SigningMethodRSA); !ok {
return nil, fmt.Errorf("Unexpected signing method: %v", token.Header["alg"])
}
if token.Header["alg"] != "RS512" {
return nil, fmt.Errorf("Unexpected signing method: %v", token.Header["alg"])
}
return key, nil
})
if token.Valid {
if claims, ok := token.Claims.(jwt.MapClaims); ok {
return claims, nil
} else {
return nil, fmt.Errorf("Token claims error %+v", err)
}
} else if ve, ok := err.(*jwt.ValidationError); ok {
if ve.Errors&jwt.ValidationErrorMalformed != 0 {
return nil, fmt.Errorf("Token is malformed %+v", ve)
} else if ve.Errors&(jwt.ValidationErrorExpired|jwt.ValidationErrorNotValidYet) != 0 {
return nil, fmt.Errorf("Token is expired %+v", ve)
} else {
return nil, fmt.Errorf("Token had an unknown error %+v", ve)
}
} else {
// Unknown error
return nil, fmt.Errorf("An unknown error occurred validating the token. That's all we know")
}
}
func CreateContext(oauth bool, nfId, NrfUri, reqNf string) context.Context {
var c context.Context
additional_params := url.Values{}
additional_params.Add("OAuth", strconv.FormatBool(oauth))
if oauth == true {
additional_params.Add("grant_type", "client_credentials")
additional_params.Add("nfInstanceId", nfId)
additional_params.Add("NrfUri", NrfUri)
additional_params.Add("nfType", reqNf)
}
c = context.WithValue(context.Background(), ContextOAuthAdditionalParams, additional_params)
return c
}