-
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.
Merge pull request #30 from EringiShimeji/auth/auth-interceptor
エンドポイントの保護
- Loading branch information
Showing
9 changed files
with
230 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,62 @@ | ||
package interceptor | ||
|
||
import ( | ||
"connectrpc.com/connect" | ||
"context" | ||
"errors" | ||
"github.com/google/uuid" | ||
"google.golang.org/protobuf/proto" | ||
"google.golang.org/protobuf/reflect/protoreflect" | ||
"google.golang.org/protobuf/types/descriptorpb" | ||
"log" | ||
commonv1 "sudoku/gen/sudoku/common/v1" | ||
authH "sudoku/handler/auth" | ||
authS "sudoku/service/auth" | ||
) | ||
|
||
// TODO: streaming の場合は別の interceptor が必要かも | ||
func NewAuthInterceptor(authService authS.IAuthService) connect.UnaryInterceptorFunc { | ||
interceptor := func(next connect.UnaryFunc) connect.UnaryFunc { | ||
return connect.UnaryFunc(func( | ||
ctx context.Context, | ||
req connect.AnyRequest, | ||
) (connect.AnyResponse, error) { | ||
methodDesc, ok := req.Spec().Schema.(protoreflect.MethodDescriptor) | ||
if !ok { | ||
return nil, connect.NewError(connect.CodeInternal, errors.New("invalid method descriptor")) | ||
} | ||
opts := methodDesc.Options().(*descriptorpb.MethodOptions) | ||
requireAuth, ok := proto.GetExtension(opts, commonv1.E_RequireAuth).(bool) | ||
if !ok { | ||
return nil, connect.NewError(connect.CodeInternal, errors.New("invalid method option")) | ||
} | ||
|
||
log.Println("requireAuth", requireAuth) | ||
|
||
if requireAuth { | ||
sessionCookie := req.Header().Get(authH.SessionCookieName) | ||
if sessionCookie == "" { | ||
return nil, connect.NewError(connect.CodeUnauthenticated, errors.New("missing session cookie")) | ||
} | ||
sessionID, err := uuid.Parse(sessionCookie) | ||
if err != nil { | ||
return nil, connect.NewError(connect.CodeUnauthenticated, errors.New("invalid session cookie")) | ||
} | ||
|
||
output, err := authService.ValidateSession(authS.ValidateSessionInput{ | ||
SessionID: sessionID, | ||
}) | ||
if err != nil { | ||
return nil, connect.NewError(connect.CodeInternal, err) | ||
} | ||
|
||
if !output.IsValid { | ||
return nil, connect.NewError(connect.CodeUnauthenticated, errors.New("invalid session")) | ||
} | ||
} | ||
|
||
return next(ctx, req) | ||
}) | ||
} | ||
return connect.UnaryInterceptorFunc(interceptor) | ||
} |
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,25 @@ | ||
// | ||
// Generated code. Do not modify. | ||
// source: sudoku/common/v1/options.proto | ||
// | ||
// @dart = 2.12 | ||
|
||
// ignore_for_file: annotate_overrides, camel_case_types, comment_references | ||
// ignore_for_file: constant_identifier_names, library_prefixes | ||
// ignore_for_file: non_constant_identifier_names, prefer_final_fields | ||
// ignore_for_file: unnecessary_import, unnecessary_this, unused_import | ||
|
||
import 'dart:core' as $core; | ||
|
||
import 'package:protobuf/protobuf.dart' as $pb; | ||
|
||
class Options { | ||
static final requireAuth = $pb.Extension<$core.bool>(_omitMessageNames ? '' : 'google.protobuf.MethodOptions', _omitFieldNames ? '' : 'requireAuth', 50000, $pb.PbFieldType.OB); | ||
static void registerAllExtensions($pb.ExtensionRegistry registry) { | ||
registry.add(requireAuth); | ||
} | ||
} | ||
|
||
|
||
const _omitFieldNames = $core.bool.fromEnvironment('protobuf.omit_field_names'); | ||
const _omitMessageNames = $core.bool.fromEnvironment('protobuf.omit_message_names'); |
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,11 @@ | ||
// | ||
// Generated code. Do not modify. | ||
// source: sudoku/common/v1/options.proto | ||
// | ||
// @dart = 2.12 | ||
|
||
// ignore_for_file: annotate_overrides, camel_case_types, comment_references | ||
// ignore_for_file: constant_identifier_names, library_prefixes | ||
// ignore_for_file: non_constant_identifier_names, prefer_final_fields | ||
// ignore_for_file: unnecessary_import, unnecessary_this, unused_import | ||
|
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,15 @@ | ||
// | ||
// Generated code. Do not modify. | ||
// source: sudoku/common/v1/options.proto | ||
// | ||
// @dart = 2.12 | ||
|
||
// ignore_for_file: annotate_overrides, camel_case_types, comment_references | ||
// ignore_for_file: constant_identifier_names, library_prefixes | ||
// ignore_for_file: non_constant_identifier_names, prefer_final_fields | ||
// ignore_for_file: unnecessary_import, unnecessary_this, unused_import | ||
|
||
import 'dart:convert' as $convert; | ||
import 'dart:core' as $core; | ||
import 'dart:typed_data' as $typed_data; | ||
|
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,9 @@ | ||
syntax = "proto3"; | ||
|
||
package sudoku.common.v1; | ||
|
||
import "google/protobuf/descriptor.proto"; | ||
|
||
extend google.protobuf.MethodOptions { | ||
bool require_auth = 50000; | ||
} |