user3500462
user3500462

Reputation: 135

ServiceStack and OAuth2

How can I use the existing servicestack oauth2 providers, google for example, and only limit it to one account that I create for my users?

Basically, I want to keep the Api access under check, so that not everyone who has a google account can use it.

Upvotes: 1

Views: 59

Answers (1)

mythz
mythz

Reputation: 143284

You can use the CustomValidationFilter to add your own Custom Validation, returning a non null response will cancel Authentication and return the desired error response, e.g:

Plugins.Add(new AuthFeature(() => new CustomUserSession(),
    new IAuthProvider[] {
        new GoogleOAuth2Provider(appSettings) { 
            CustomValidationFilter = authCtx => {
                if (!AllowUser(authCtx.Session, authCtx.AuthTokens)) {
                    var url = authCtx.AuthProvider.GetReferrerUrl(
                        authCtx.Service, authCtx.Session
                    ).AddParam("f","GoogleOAuthNotAllowed");
                    return authCtx.Service.Redirect(url);
                }
                return null; //Allow Authentication through
            }
        }, 
    }));

Upvotes: 1

Related Questions