-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
762d94b
commit ff43bfc
Showing
35 changed files
with
2,267 additions
and
496 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package auth | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"github.com/golang-jwt/jwt/v5" | ||
"github.com/google/uuid" | ||
"google.golang.org/grpc" | ||
"google.golang.org/grpc/codes" | ||
"google.golang.org/grpc/metadata" | ||
"google.golang.org/grpc/status" | ||
"monify/lib" | ||
"net/http" | ||
"strings" | ||
) | ||
|
||
type AuthMiddleware struct { | ||
JwtSecret string | ||
} | ||
|
||
func (m AuthMiddleware) HttpMiddleware(next http.Handler) http.Handler { | ||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
auth := r.Header.Get("Authorization") | ||
if auth != "" { | ||
token, err := validateBearerToken(auth, m.JwtSecret) | ||
if err != nil { | ||
http.Error(w, err.Error(), http.StatusUnauthorized) | ||
return | ||
} | ||
ctx := context.WithValue(r.Context(), lib.UserIdContextKey{}, token) | ||
r = r.WithContext(ctx) | ||
} | ||
next.ServeHTTP(w, r) | ||
}) | ||
} | ||
|
||
func (m AuthMiddleware) GrpcExtractUserId(ctx context.Context, req any, info *grpc.UnaryServerInfo) (uuid.UUID, error) { | ||
md, exists := metadata.FromIncomingContext(ctx) | ||
if !exists { | ||
return uuid.Nil, nil | ||
} | ||
auths := md.Get("authorization") | ||
if len(auths) == 0 { | ||
return uuid.Nil, nil | ||
} | ||
auth := auths[0] | ||
token, err := validateBearerToken(auth, m.JwtSecret) | ||
if err != nil { | ||
return uuid.Nil, status.Error(codes.Unauthenticated, err.Error()) | ||
} | ||
return token, nil | ||
|
||
} | ||
|
||
func validateBearerToken(token string, secret string) (uuid.UUID, error) { | ||
if strings.HasPrefix(token, "Bearer ") { | ||
tokenStr := token[7:] | ||
token, err := jwt.ParseWithClaims(tokenStr, &jwt.RegisteredClaims{}, func(token *jwt.Token) (interface{}, error) { | ||
return []byte(secret), nil | ||
}) | ||
if claims, ok := token.Claims.(*jwt.RegisteredClaims); ok { | ||
userId, err := uuid.Parse(claims.Subject) | ||
if err != nil { | ||
return uuid.Nil, errors.New("invalid user id in token") | ||
} | ||
return userId, nil | ||
} else { | ||
switch err { | ||
case nil: | ||
return uuid.Nil, errors.New("invalid token") | ||
default: | ||
return uuid.Nil, err | ||
} | ||
} | ||
} | ||
return uuid.Nil, errors.New("invalid token") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package media | ||
|
||
type ImageStorage interface { | ||
// Store | ||
// Stores the image in the storage and returns the URL | ||
// if image id is empty, a random one will be generated | ||
Store(fileSuffix string, imageData []byte, imageId string) (string, error) | ||
|
||
Delete(path string) error | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package media | ||
|
||
type Usage int | ||
|
||
const ( | ||
Undefined Usage = iota | ||
UserAvatar | ||
GroupAvatar | ||
) | ||
|
||
func Parse(str string) Usage { | ||
switch str { | ||
case "userAvatar": | ||
return UserAvatar | ||
case "groupAvatar": | ||
return GroupAvatar | ||
default: | ||
return Undefined | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package media | ||
|
||
import ( | ||
"github.com/google/uuid" | ||
"time" | ||
) | ||
|
||
type TmpImage struct { | ||
Id uuid.UUID | ||
ExpectedUsage Usage | ||
Uploader uuid.UUID | ||
UploadedAt time.Time | ||
URL string | ||
} | ||
|
||
type ConfirmedImage struct { | ||
Id uuid.UUID | ||
Usage Usage | ||
Uploader uuid.UUID | ||
UploadedAt time.Time | ||
URL string | ||
ConfirmedAt time.Time | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
DROP TABLE TmpImage; | ||
DROP TABLE ConfirmedImage; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
CREATE TABLE TmpImage( | ||
imgId uuid PRIMARY KEY, | ||
url varchar(100) NOT NULL , | ||
expected_usage int8 NOT NULL , | ||
uploader uuid references user_identity(user_id) NOT NULL, | ||
uploaded_at timestamp NOT NULL | ||
); | ||
|
||
CREATE TABLE ConfirmedImage( | ||
imgId uuid PRIMARY KEY, | ||
url varchar(100) NOT NULL , | ||
usage int8 NOT NULL , | ||
uploader uuid references user_identity(user_id) NOT NULL, | ||
uploaded_at timestamp NOT NULL, | ||
confirmed_at timestamp NOT NULL | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.