Skip to content

Commit d1ca0a6

Browse files
committed
initial version
0 parents  commit d1ca0a6

File tree

8 files changed

+202
-0
lines changed

8 files changed

+202
-0
lines changed

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2025 ZARKONES
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# INTRODUCTION
2+
This is library for authorization and access control via public-key cryptography.

error-handler.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package access
2+
3+
var ErrorHandler = func(userID string, err error) {}

go.mod

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module access
2+
3+
go 1.24.5
4+
5+
require github.com/golang-jwt/jwt/v5 v5.3.0

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
github.com/golang-jwt/jwt/v5 v5.3.0 h1:pv4AsKCKKZuqlgs5sUmn4x8UlGa0kEVt/puTpKx9vvo=
2+
github.com/golang-jwt/jwt/v5 v5.3.0/go.mod h1:fxCRLWMO43lRc8nhHWY6LGqRcf+1gQWArsqaEUEa5bE=

helpers.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package access
2+
3+
import (
4+
"crypto/rsa"
5+
"net/http"
6+
)
7+
8+
func AuthenticateAndAuthorize(
9+
w http.ResponseWriter,
10+
r *http.Request,
11+
permissionKey PermissionKey,
12+
metadata *string,
13+
getPublicKeyByUserID func(userID string) (*rsa.PublicKey, error),
14+
getPermissionsByUserID func(userID string) ([]Permission, error),
15+
) (username string, permissions []Permission, reject bool) {
16+
username, permissions, reject = Authenticate(w, r, getPublicKeyByUserID, getPermissionsByUserID)
17+
if reject {
18+
return username, permissions, reject
19+
}
20+
21+
if permissionKey != PERMISSION_NOT_SPECIFIED {
22+
if reject := Authorize(permissionKey, metadata, permissions); reject {
23+
http.Error(w, "", http.StatusUnauthorized)
24+
return "", nil, true
25+
}
26+
}
27+
28+
return username, permissions, false
29+
}
30+
31+
func Authenticate(
32+
w http.ResponseWriter,
33+
r *http.Request,
34+
getPublicKeyByUserID func(userID string) (*rsa.PublicKey, error),
35+
getPermissionsByUserID func(userID string) ([]Permission, error),
36+
) (userID string, permissions []Permission, reject bool) {
37+
38+
token := r.Header.Get("Authorization")
39+
40+
userID, err := VerifyToken(token, getPublicKeyByUserID)
41+
if err != nil {
42+
ErrorHandler(userID, err)
43+
http.Error(w, "", http.StatusUnauthorized)
44+
return "", nil, true
45+
}
46+
47+
permissions, err = getPermissionsByUserID(userID)
48+
if err != nil {
49+
ErrorHandler(userID, err)
50+
http.Error(w, "", http.StatusUnauthorized)
51+
return "", nil, true
52+
}
53+
54+
return userID, permissions, false
55+
}
56+
57+
func Authorize(permissionKey PermissionKey, metadata *string, permissions []Permission) (reject bool) {
58+
for _, permission := range permissions {
59+
if permission.Key != permissionKey {
60+
continue
61+
}
62+
if metadata != nil && *metadata != permission.Metadata {
63+
continue
64+
}
65+
66+
return false
67+
}
68+
69+
return true
70+
}

token.go

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package access
2+
3+
import (
4+
"crypto/rsa"
5+
"encoding/base64"
6+
"encoding/json"
7+
"errors"
8+
"fmt"
9+
"strings"
10+
11+
"github.com/golang-jwt/jwt/v5"
12+
)
13+
14+
var (
15+
ErrMissingAuthHeader = errors.New("missing Authorization header")
16+
ErrInvalidJWTSegments = errors.New("invalid size of jwt segments")
17+
ErrNilInsecurePayload = errors.New("insecure parsed token payload appears as nil")
18+
ErrInvalidInsecureUserID = errors.New("insecure parsed token user id is invalid")
19+
ErrInvalidSigningAlg = errors.New("invalid signing algorithm")
20+
ErrInvalidSig = errors.New("invalid token's signature")
21+
ErrInvalidClaims = errors.New("claims casting did not go well")
22+
ErrInvalidPubKeyPtr = errors.New("invalid pointer to public key")
23+
ErrInvalidUserID = errors.New("user id is invalid")
24+
)
25+
26+
func VerifyToken(rawToken string, getPublicKeyByUserID func(userID string) (*rsa.PublicKey, error)) (userID string, err error) {
27+
if rawToken == "" {
28+
return "", ErrMissingAuthHeader
29+
}
30+
if getPublicKeyByUserID == nil {
31+
return "", ErrInvalidPubKeyPtr
32+
}
33+
34+
segments := strings.Split(rawToken, ".")
35+
if len(segments) != 3 {
36+
return "", ErrInvalidJWTSegments
37+
}
38+
39+
insecurePayloadJsonStr, err := base64.RawStdEncoding.DecodeString(segments[1])
40+
if err != nil {
41+
return "", err
42+
}
43+
44+
var insecurePayload map[string]any
45+
46+
if err := json.Unmarshal(insecurePayloadJsonStr, &insecurePayload); err != nil {
47+
return "", err
48+
}
49+
50+
if insecurePayload == nil {
51+
return "", ErrNilInsecurePayload
52+
}
53+
54+
insecureUserID := fmt.Sprint(insecurePayload["u"])
55+
if len(insecureUserID) == 0 || len(insecureUserID) > 32 {
56+
return "", ErrInvalidInsecureUserID
57+
}
58+
59+
token, err := jwt.Parse(rawToken, func(t *jwt.Token) (any, error) {
60+
if _, ok := t.Method.(*jwt.SigningMethodRSA); !ok {
61+
return nil, ErrInvalidSigningAlg
62+
}
63+
64+
return getPublicKeyByUserID(insecureUserID)
65+
})
66+
if err != nil {
67+
return "", err
68+
}
69+
70+
if !token.Valid {
71+
return "", ErrInvalidSig
72+
}
73+
74+
claims, ok := token.Claims.(jwt.MapClaims)
75+
if !ok {
76+
return "", ErrInvalidClaims
77+
}
78+
79+
verifiedUserID := fmt.Sprint(claims["id"])
80+
if len(verifiedUserID) == 0 {
81+
return "", ErrInvalidUserID
82+
}
83+
84+
return verifiedUserID, nil
85+
}

types.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package access
2+
3+
type PermissionKey int
4+
5+
const (
6+
PERMISSION_NOT_SPECIFIED = PermissionKey(iota)
7+
)
8+
9+
type Permission struct {
10+
Key PermissionKey
11+
UserID string
12+
Metadata string
13+
CreatedAt int64
14+
}

0 commit comments

Comments
 (0)