|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include <ydb/public/api/client/yc_private/oauth/session_service.grpc.pb.h> |
| 4 | +#include <ydb/public/api/client/yc_private/iam/iam_token_service.grpc.pb.h> |
| 5 | +#include <util/generic/hash_set.h> |
| 6 | +#include <util/generic/hash.h> |
| 7 | + |
| 8 | +class TSessionServiceMock : public yandex::cloud::priv::oauth::v1::SessionService::Service { |
| 9 | + yandex::cloud::priv::oauth::v1::AuthorizationRequired AuthorizationRequiredMessage; |
| 10 | + |
| 11 | + THashMap<TString, TString> ParseCookie(TStringBuf cookie) { |
| 12 | + THashMap<TString, TString> parsedCookies; |
| 13 | + for (TStringBuf param = cookie.NextTok(';'); !param.empty(); param = cookie.NextTok(';')) { |
| 14 | + param.SkipPrefix(" "); |
| 15 | + TStringBuf name = param.NextTok('='); |
| 16 | + parsedCookies[name] = param; |
| 17 | + } |
| 18 | + return parsedCookies; |
| 19 | + } |
| 20 | + |
| 21 | +public: |
| 22 | + std::pair<const TString, TString> AllowedCookies {"yc_session", "allowed_session_cookie"}; |
| 23 | + bool IsTokenAllowed {true}; |
| 24 | + bool IsOpenIdScopeMissed {false}; |
| 25 | + THashSet<TString> AllowedAccessTokens; |
| 26 | + |
| 27 | + TSessionServiceMock() { |
| 28 | + AuthorizationRequiredMessage.Setauthorize_url("https://auth.cloud.yandex.ru/oauth/authorize"); |
| 29 | + } |
| 30 | + |
| 31 | + grpc::Status Check(grpc::ServerContext*, |
| 32 | + const yandex::cloud::priv::oauth::v1::CheckSessionRequest* request, |
| 33 | + yandex::cloud::priv::oauth::v1::CheckSessionResponse* response) override { |
| 34 | + if (!IsTokenAllowed) { |
| 35 | + return grpc::Status(grpc::StatusCode::UNAUTHENTICATED, "Authorization IAM token are invalid or may have expired"); |
| 36 | + } |
| 37 | + const THashMap<TString, TString> cookies = ParseCookie(request->Getcookie_header()); |
| 38 | + auto it = cookies.find(AllowedCookies.first); |
| 39 | + if (it != cookies.cend()) { |
| 40 | + if (it->second == AllowedCookies.second) { |
| 41 | + auto iam_token = response->Mutableiam_token(); |
| 42 | + iam_token->Setiam_token("protected_page_iam_token"); |
| 43 | + return grpc::Status(grpc::StatusCode::OK, "Cookie is corrected"); |
| 44 | + } |
| 45 | + } |
| 46 | + const TString errorDetailsPrefix = "Error details perfix\n"; |
| 47 | + const TString errorDetailsSuffix = "\nError details suffix"; |
| 48 | + return grpc::Status(grpc::StatusCode::INVALID_ARGUMENT, |
| 49 | + "The provided cookies are invalid or may have expired", |
| 50 | + errorDetailsPrefix + AuthorizationRequiredMessage.SerializeAsString() + errorDetailsSuffix); |
| 51 | + } |
| 52 | + |
| 53 | + grpc::Status Create(grpc::ServerContext*, |
| 54 | + const yandex::cloud::priv::oauth::v1::CreateSessionRequest* request, |
| 55 | + yandex::cloud::priv::oauth::v1::CreateSessionResponse* response) override { |
| 56 | + if (!IsTokenAllowed) { |
| 57 | + return grpc::Status(grpc::StatusCode::UNAUTHENTICATED, "Authorization IAM token are invalid or may have expired"); |
| 58 | + } |
| 59 | + if (IsOpenIdScopeMissed) { |
| 60 | + return grpc::Status(grpc::StatusCode::FAILED_PRECONDITION, "Openid scope is missed for specified access_token"); |
| 61 | + } |
| 62 | + if (AllowedAccessTokens.count(request->Getaccess_token()) > 0) { |
| 63 | + response->Addset_cookie_header(AllowedCookies.first + "=" + AllowedCookies.second + "; SameSite=Lax"); |
| 64 | + return grpc::Status(grpc::StatusCode::OK, "Cookie was created"); |
| 65 | + } |
| 66 | + return grpc::Status(grpc::StatusCode::INVALID_ARGUMENT, "The provided access_token is invalid or may have expired", AuthorizationRequiredMessage.SerializeAsString()); |
| 67 | + } |
| 68 | +}; |
0 commit comments