Passthrough Authentication in ServiceStack

I have two ServiceStack X and Y servers. Server X has user registration and authentication features. It has the functions RegistrationFeature, CredentialsAuthProvider, MemoryCacheClient and MongoDbAuthRepository for authentication processing. I recently introduced server Y and GUI forms that talk to server Y to handle another part of my business domain. Server Y needs to query authenticated endpoints on server X.

How to configure server Y so that when it receives login requests from GUI forms, it transfers this responsibility to server X, which has access to user information?

I tried implementing custom CredentialsAuthProvider on server Y like this:

public override bool TryAuthenticate(IServiceBase authService, string userName, string password)
{
    // authenticate through server X
    try
    {
        var client = new JsonServiceClient("http://localhost:8088");
        var createRequest = new Authenticate
        {
            UserName = userName,
            Password = password,
            provider = Name,
        };

        var authResponse = client.Post(createRequest);
        return true;
    }
    catch (WebServiceException ex)
    {
        // "Unauthorized
        return false;
    }
}

, Y X, .

public class MyServices2 : Service
{
    public object Any(TwoPhase request)
    {
        try
        {
            // make a request to server X on an authenticated endpoint
            var client = new JsonServiceClient("http://localhost:8088");

            var helloRequest = new Hello
            {
                Name = "user of server Y"
            };

            var response = client.Post(helloRequest);

            return new TwoPhaseResponse { Result = $"Server X says: {response.Result}" };
        }
        catch (WebServiceException e)
        {
            Console.WriteLine(e);
            throw;
        }
    }

    ...
}
+6
1

. CredentialsAuthProvider, , (.. , MemoryCacheClient). - , , cookie , , Service Client, . ServiceStack, cookie , .

ServiceStack , Cookie - :

public object Any(ClientRequest request)
{
    // make a request to server X on an authenticated endpoint
    var session = base.SessionAs<AuthUserSession>();
    var client = new JsonServiceClient("http://localhost:8088");
    client.SetSessionId(session.Id);

    var response = client.Post(new Hello {
        Name = "user of server Y"
    });

    return new TwoPhaseResponse { Result = $"Server X says: {response.Result}" };
}

BasicAuthProvider

, HTTP Basic Auth BasicAuthProvider, UserName/Password , :

var basicAuth = base.Request.GetBasicAuthUserAndPassword();
client.UserName = basicAuth.Value.Key;
client.Password = basicAuth.Value.Value;
client.AlwaysSendBasicAuthHeader = true;

/, , . , ServiceStack BasicAuthProvider User Auth Repository, /.

API

API- AuthProvider, - , UserName/Password API

var apikey = base.Request.GetApiKey();
client.BearerToken = apikey.Id;

, Same ApiKeyAuthProvider User Auth Repository, API.

JWT AuthProvider

, , (, Caching Provider/User Auth Repository), JWT Auth Provider, , ServiceStack, JWT, ServiceStack, JwtAuthProviderReader.

JWT, :

var bearerToken = base.Request.GetBearerToken()
    ?? base.Request.GetCookieValue(Keywords.TokenCookie);

client.BearerToken = bearerToken;
+5

Source: https://habr.com/ru/post/1016462/


All Articles