-
Notifications
You must be signed in to change notification settings - Fork 1
gRPC SSO implementation #54
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
BOBAvov
wants to merge
16
commits into
v2
Choose a base branch
from
v2_vova
base: v2
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
15734b1
Add SSO configuration and GRPC client for admin checks
BOBAvov 5a71efa
Add SSO configuration and GRPC client for admin checks
BOBAvov 2f46fa2
Rename IsAdminProvider to SSOClient and fix naming inconsistencies
BOBAvov b8ac57a
rename NewIsAdminProvider to NewSSOClient
BOBAvov 3aea896
refactor: update package names and dependencies for SSO integration
BOBAvov c72aa93
refactor: update SSO client initialization and error handling
BOBAvov 804d52c
feat: add Dockerfile for building and running SSO service
BOBAvov b44b688
up Dockerfile to goland:1.25
BOBAvov 212d0d1
feat: enhance configuration management and add gRPC service to docker…
BOBAvov 602c447
feat: add AppId to SSO configuration and update SSO client initializa…
BOBAvov fcaf9e7
fix: apply suggestions from code review
zhikh23 d3ebb6b
fix: correct AppId to AppID in SSO client and update related metadata
BOBAvov f97e47c
fix: correct AppId to AppID in SSO client and update related metadata
BOBAvov 831aaec
fix: update AppId to AppID in configuration and related files, and ad…
BOBAvov a0b2019
fix: correct AppId to AppID in SSO configuration and update comments …
BOBAvov d469252
update easyp.yaml
BOBAvov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -7,3 +7,8 @@ logging: | |
|
|
||
| storage: | ||
| base_path: "uploads" | ||
|
|
||
| sso: | ||
| host: "localhost" | ||
| port: "44044" | ||
| app_id: 1 | ||
This file contains hidden or 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 |
|---|---|---|
| @@ -1,2 +1,2 @@ | ||
| github.com/googleapis/googleapis common-protos-1_3_1 h1:s8d5N7+VjksySahU4JpUGSju5SqkmQNtweRfsin9SyM= | ||
| github.com/grpc-ecosystem/grpc-gateway v0.0.0-20251127211339-d2d5e587243b4254ac96b76ce19636e0fd734a5e h1:qihtPAOYuB5MmdtIx/0GX3DZrVBCp/kZz2iolgsuOys= | ||
| github.com/googleapis/googleapis common-protos-1_3_1 h1:TsE+JW4H/zsB0phfwMO1DragQ4oaGfoVuGHV5aGfn2I= | ||
| github.com/grpc-ecosystem/grpc-gateway v0.0.0-20251127211339-d2d5e587243b4254ac96b76ce19636e0fd734a5e h1:48F3p4F2740v+ZV/mCXiBwQcK+LBbN8h+alOaIx9BD0= |
This file contains hidden or 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 |
|---|---|---|
| @@ -1,19 +1,31 @@ | ||
| version: v1alpha | ||
| lint: | ||
| use: | ||
| - DEFAULT | ||
| use: | ||
| - DEFAULT | ||
| deps: | ||
| - github.com/googleapis/googleapis@common-protos-1_3_1 | ||
| - github.com/grpc-ecosystem/grpc-gateway | ||
| - github.com/googleapis/googleapis@common-protos-1_3_1 | ||
| - github.com/grpc-ecosystem/grpc-gateway@v2.19.1 | ||
| generate: | ||
| inputs: | ||
| - directory: "api/v2" | ||
| plugins: | ||
| - name: go | ||
| out: gen/go | ||
| opt: | ||
| paths: source_relative | ||
| - name: go-grpc | ||
| out: gen/go | ||
| opt: | ||
| paths: source_relative | ||
| inputs: | ||
| - directory: "api/v2" | ||
| plugins: | ||
| - name: go | ||
| out: gen/go | ||
| opt: | ||
| paths: source_relative | ||
| module: "github.com/bmstu-itstech/scriptum-back" | ||
| with_imports: true | ||
|
|
||
| - name: go-grpc | ||
| out: gen/go | ||
| opt: | ||
| paths: source_relative | ||
|
|
||
| - name: grpc-gateway | ||
| out: gen/go | ||
| opts: | ||
| paths: source_relative | ||
|
|
||
| - name: openapiv2 | ||
| out: ./gen/openapi | ||
| opts: | ||
| simple_operation_ids: true |
This file contains hidden or 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 hidden or 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 hidden or 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 was deleted.
Oops, something went wrong.
This file contains hidden or 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,75 @@ | ||
| package sso | ||
|
|
||
| import ( | ||
| "context" | ||
| ssov1 "github.com/BOBAvov/protos_sso/gen/go/sso" | ||
| "github.com/bmstu-itstech/scriptum-back/internal/config" | ||
| "github.com/bmstu-itstech/scriptum-back/internal/domain/value" | ||
| "google.golang.org/grpc" | ||
| "google.golang.org/grpc/credentials/insecure" | ||
| "google.golang.org/grpc/metadata" | ||
| "log/slog" | ||
| "net" | ||
| "strconv" | ||
| ) | ||
|
|
||
| type SSO struct { | ||
| conn *grpc.ClientConn | ||
| api ssov1.AuthClient | ||
| l *slog.Logger | ||
| AppID int32 | ||
| } | ||
|
|
||
| func MustNewSSOClient(config config.SSO, l *slog.Logger) (*SSO, func() error) { | ||
| sso, closeFn, err := NewSSOClient(config, l) | ||
| if err != nil { | ||
| panic("Error creating SSO client: " + err.Error()) | ||
| } | ||
| return sso, closeFn | ||
| } | ||
|
|
||
| func NewSSOClient(config config.SSO, l *slog.Logger) (*SSO, func() error, error) { | ||
| addr := net.JoinHostPort(config.Host, config.Port) | ||
| cc, err := grpc.NewClient(addr, grpc.WithTransportCredentials(insecure.NewCredentials())) | ||
zhikh23 marked this conversation as resolved.
Show resolved
Hide resolved
zhikh23 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if err != nil { | ||
| return nil, func() error { return nil }, err | ||
| } | ||
|
|
||
| closeFn := cc.Close | ||
|
|
||
| return &SSO{ | ||
| api: ssov1.NewAuthClient(cc), | ||
| conn: cc, | ||
| l: l, | ||
| AppID: config.AppID, | ||
| }, closeFn, nil | ||
| } | ||
|
|
||
| // IsAdmin checks if the user with the given uid has admin privileges. | ||
| // Note: uid parameter is of type value.UserID but is converted to int64 for the gRPC call. | ||
| func (s *SSO) IsAdmin(ctx context.Context, uid value.UserID) (bool, error) { | ||
| const op = "infra.SSO.IsAdmin" | ||
|
|
||
| l := s.l.With( | ||
| slog.String("op", op), | ||
| slog.Int64("uid", int64(uid)), | ||
| ) | ||
|
|
||
| l.Debug("Checking admin status") | ||
| // Add metadata with application ID to the context | ||
| ctx = metadata.NewOutgoingContext(ctx, metadata.Pairs( | ||
| "x-user-id", strconv.FormatInt(int64(s.AppID), 10), | ||
BOBAvov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| )) | ||
| // Call the IsAdmin method on the SSO server | ||
| resp, err := s.api.IsAdmin(ctx, &ssov1.IsAdminRequest{ | ||
| UserId: int64(uid), | ||
| }) | ||
| if err != nil { | ||
| l.Error("Failed to check admin status", slog.String("error", err.Error())) | ||
| return false, err | ||
| } | ||
|
|
||
| l.Debug("Admin status", slog.Bool("is_admin", resp.IsAdmin)) | ||
|
|
||
| return resp.IsAdmin, nil | ||
| } | ||
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.