public OAuth2Message Issue(string securityTokenServiceUrl, OAuth2AccessTokenRequest oauth2Request) { OAuth2Message oAuth2Message; OAuth2WebRequest oAuth2WebRequest = new OAuth2WebRequest(securityTokenServiceUrl, oauth2Request); try { WebResponse response = oAuth2WebRequest.GetResponse(); oAuth2Message = OAuth2MessageFactory.CreateFromEncodedResponse(new StreamReader(response.GetResponseStream())); } catch (Exception exception) { throw new RequestFailedException("Token request failed.", exception); } return oAuth2Message; }
//public static OAuth2AccessTokenRequest CreateAccessTokenRequestWithAssertion(SecurityToken token, SecurityTokenHandlerCollection securityTokenHandlers, string resource) //{ // if (token == null) { throw new ArgumentException("token"); } // if (token is JwtSecurityToken) // { // return OAuth2MessageFactory.CreateAccessTokenRequestWithAssertion((JwtSecurityToken)token, securityTokenHandlers, resource); // } // //if (token is GenericXmlSecurityToken) // //{ // // return OAuth2MessageFactory.CreateAccessTokenRequestWithAssertion((GenericXmlSecurityToken)token, resource); // //} // //if (!(token is SamlSecurityToken) && !(token is Saml2SecurityToken)) // //{ // // throw new ArgumentException("Unsupported SecurityToken"); // //} // return OAuth2MessageFactory.CreateAccessTokenRequestWithAssertionForSamlSecurityTokens(token, securityTokenHandlers, resource); //} // private static OAuth2AccessTokenRequest CreateAccessTokenRequestWithAssertion(GenericXmlSecurityToken token, string resource) // { // string str; // if (token == null) { throw new ArgumentException("token"); } // OAuth2AccessTokenRequest oAuth2AccessTokenRequest = new OAuth2AccessTokenRequest(); // JwtSecurityTokenHandler jsonWebSecurityTokenHandler = new JwtSecurityTokenHandler(); //// XmlReader xmlNodeReader = new XmlNodeReader(token.TokenXml); // string jsonTokenString = jsonWebSecurityTokenHandler.GetJsonTokenString(xmlNodeReader, out str); // oAuth2AccessTokenRequest.GrantType = OAuth2MessageFactory.GetTokenType(token); // oAuth2AccessTokenRequest.Assertion = jsonTokenString; // oAuth2AccessTokenRequest.Resource = resource; // return oAuth2AccessTokenRequest; // } //private static OAuth2AccessTokenRequest CreateAccessTokenRequestWithAssertion(JwtSecurityToken token, SecurityTokenHandlerCollection securityTokenHandlers, string resource) private static OAuth2AccessTokenRequest CreateAccessTokenRequestWithAssertion(JwtSecurityToken token, JwtSecurityTokenHandler handler, string resource) { if (token == null) { throw new ArgumentException("token"); } if (handler== null) { throw new ArgumentException("securityTokenHandlers"); } JwtSecurityTokenHandler item = handler;//securityTokenHandlers[typeof(JwtSecurityToken)] as JwtSecurityTokenHandler; if (item == null) { throw new ArgumentException("The input security token handlers collection does not contain a handler for JWT tokens.", "securityTokenHandlers"); } string str = item.WriteToken(token); OAuth2AccessTokenRequest oAuth2AccessTokenRequest = new OAuth2AccessTokenRequest() { GrantType = "http://oauth.net/grant_type/jwt/1.0/bearer", Assertion = str, Resource = resource }; return oAuth2AccessTokenRequest; }
public static OAuth2AccessTokenRequest CreateAccessTokenRequestWithRefreshToken(string clientId, string clientSecret, string refreshToken, string resource) { OAuth2AccessTokenRequest oAuth2AccessTokenRequest = new OAuth2AccessTokenRequest() { GrantType = "refresh_token", ClientId = clientId, ClientSecret = clientSecret, RefreshToken = refreshToken, Resource = resource }; return oAuth2AccessTokenRequest; }
public static OAuth2AccessTokenRequest CreateAccessTokenRequestWithClientCredentials(string clientId, string clientSecret, string scope) { OAuth2AccessTokenRequest oAuth2AccessTokenRequest = new OAuth2AccessTokenRequest() { GrantType = "client_credentials", ClientId = clientId, ClientSecret = clientSecret, Scope = scope }; return oAuth2AccessTokenRequest; }
public static OAuth2AccessTokenRequest CreateAccessTokenRequestWithAuthorizationCode(string clientId, string clientSecret, string authorizationCode, string resource) { OAuth2AccessTokenRequest oAuth2AccessTokenRequest = new OAuth2AccessTokenRequest() { GrantType = "authorization_code", ClientId = clientId, ClientSecret = clientSecret, Code = authorizationCode, Resource = resource }; return oAuth2AccessTokenRequest; }
//private static OAuth2AccessTokenRequest CreateAccessTokenRequestWithAssertionForSamlSecurityTokens(SecurityToken token, SecurityTokenHandlerCollection securityTokenHandlers, string resource) //{ // if (securityTokenHandlers == null) { throw new ArgumentException("securityTokenHandlers"); } // OAuth2AccessTokenRequest oAuth2AccessTokenRequest = new OAuth2AccessTokenRequest(); // if (!(token is SamlSecurityToken)) // { // oAuth2AccessTokenRequest.GrantType = "urn:oasis:names:tc:SAML:2.0:assertion"; // } // else // { // oAuth2AccessTokenRequest.GrantType = "urn:oasis:names:tc:SAML:1.0:assertion"; // } // XmlWriterSettings xmlWriterSetting = new XmlWriterSettings(); // StringBuilder stringBuilder = new StringBuilder(); // xmlWriterSetting.OmitXmlDeclaration = true; // using (XmlWriter xmlWriter = XmlWriter.Create(stringBuilder, xmlWriterSetting)) // { // securityTokenHandlers.WriteToken(xmlWriter, token); // oAuth2AccessTokenRequest.Assertion = stringBuilder.ToString(); // } // oAuth2AccessTokenRequest.Resource = resource; // return oAuth2AccessTokenRequest; //} public static OAuth2AccessTokenRequest CreateAccessTokenRequestWithAuthorizationCode(string clientId, string clientSecret, string authorizationCode, Uri redirectUri, string resource) { OAuth2AccessTokenRequest oAuth2AccessTokenRequest = new OAuth2AccessTokenRequest() { GrantType = "authorization_code", ClientId = clientId, ClientSecret = clientSecret, Code = authorizationCode }; if (redirectUri != null) { oAuth2AccessTokenRequest.RedirectUri = redirectUri.AbsoluteUri; } oAuth2AccessTokenRequest.Resource = resource; return oAuth2AccessTokenRequest; }
public static OAuth2AccessTokenRequest Read(string requestString) { OAuth2AccessTokenRequest oAuth2AccessTokenRequest = new OAuth2AccessTokenRequest(); try { oAuth2AccessTokenRequest.Decode(requestString); } catch (Exception invalidRequestException) { //if (string.IsNullOrEmpty(nameValueCollection["client_id"]) && string.IsNullOrEmpty(nameValueCollection["assertion"])) throw new InvalidDataException("The request body must contain a client_id or assertion parameter."); } foreach (string key in oAuth2AccessTokenRequest.Keys) { if (!OAuth2AccessTokenRequest.TokenResponseParameters.Contains(key)) { continue; } throw new InvalidDataException(); } return oAuth2AccessTokenRequest; }