Last active
March 14, 2018 17:51
-
-
Save charlessolar/4826cd1af0fabfac5b8f to your computer and use it in GitHub Desktop.
Servicestack OAuth JWT Verification
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.GlobalRequestFilters.Add((httpReq, httpResp, requestDto) => | |
{ | |
var header = httpReq.Headers["Authorization"]; | |
if( header.IsNullOrEmpty() ) { | |
httpResp.StatusCode = (int)HttpStatusCode.Unauthorized; | |
httpResp.EndRequest(); | |
} | |
try | |
{ | |
var token = header.Split(' '); | |
if( token[0].ToUpper() != "BEARER" ){ | |
httpResp.StatusCode = (int)HttpStatusCode.Unauthorized; | |
httpResp.EndRequest(); | |
} | |
var secret = appSettings.GetString("oauth.auth0.AppSecret").Replace('-', '+').Replace('_', '/'); | |
JWT.JsonWebToken.Decode(token[1], Convert.FromBase64String(secret), verify: true); | |
} | |
catch (Exception) | |
{ | |
httpResp.StatusCode = (int)HttpStatusCode.Unauthorized; | |
httpResp.EndRequest(); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment