public Authentication(HttpRequest httpRequest, IAuthenticationModes iAuthenticationMode) { if (iAuthenticationMode is BasicAuth basicAuth) { string basicrheader = httpRequest.Headers["Authorization"]; if (basicrheader == null) { throw new HttpException(401, "no authorization"); } if (!basicrheader.StartsWith("Basic ")) { throw new HttpException(401, "not a basic authorization"); } var encoding = Encoding.GetEncoding("iso-8859-1"); string credentials = encoding.GetString(Convert.FromBase64String(basicrheader.Substring(6))); if (credentials != string.Format("{0}:{1}", basicAuth.Username, basicAuth.Password)) { throw new HttpException(401, "invalid token"); } authenticationMode = AuthenticationMode.BasicAuth; return; // call is authenticated } if (iAuthenticationMode is HTTPHeader httpHeader) { string bearerheader = httpRequest.Headers["Authorization"]; if (bearerheader == null) { throw new HttpException(401, "no authorization"); } if (!bearerheader.StartsWith("Bearer ")) { throw new HttpException(401, "not a bearer authorization"); } if (bearerheader != "Bearer " + httpHeader.Token) { throw new HttpException(401, "invalid token"); } authenticationMode = AuthenticationMode.HTTPHeader; return; // call is authenticated } if (iAuthenticationMode is OAUth2) { //_authenticationMode = AuthenticationMode.OAuth2; throw new Exception("OAuth authentication is not yet implemented"); } }
public void Authenticate(IAuthenticationModes authenticationMode) { authenticate = new Authentication(_httpRequest, authenticationMode); Authenticated = true; }