/// <summary> /// Requests a new OAuth token from the Authorization service /// </summary> /// <param name="context">The context to sue for cookie access</param> /// <returns>The OAuth token</returns> public static async Task <IFOAuthAccess> RefreshAccessAsync(IOwinContext context) { try { var authOptions = IFOAuthOptions.Construct(); string currentRefreshToken = context.Request.Cookies["iflo_refresh_token"]; if (currentRefreshToken != null) { var oauth2Token = await RefreshAccessTokenAsync(currentRefreshToken, authOptions); IFOAuthAccess access = new IFOAuthAccess(oauth2Token); access.Persist(context); return(access); } } catch (Exception) { // TODO handle exceptions } return(null); }
protected override async Task <AuthenticationTicket> AuthenticateCoreAsync() { AuthenticationProperties properties = null; try { string code = null; string state = null; IReadableStringCollection query = Request.Query; IList <string> values = query.GetValues("code"); code = values[0]; values = query.GetValues("state"); if (values != null && values.Count == 1) { state = values[0]; } properties = Options.StateDataFormat.Unprotect(state); var oauth2Token = await GetOAuthTokenAsync(code); var access = new IFOAuthAccess(oauth2Token); if (string.IsNullOrWhiteSpace(access.AccessToken)) { return(new AuthenticationTicket(null, properties)); } var accountInformation = await GetUserAccountInformation(access.AccessToken); var context = new IFOAuthContext(Context, accountInformation, access); context.Identity = new ClaimsIdentity( new[] { new Claim(ClaimTypes.NameIdentifier, context.Name, ClaimValueTypes.String, Options.AuthenticationType), // TODO need this id back from user info service new Claim(ClaimTypes.Name, context.Name, ClaimValueTypes.String, Options.AuthenticationType) }, Options.AuthenticationType, ClaimsIdentity.DefaultNameClaimType, ClaimsIdentity.DefaultRoleClaimType); context.Properties = properties; access.Persist(Context); await Options.Provider.Authenticated(context); return(new AuthenticationTicket(context.Identity, context.Properties)); } catch (Exception ex) { // TODO handle exception return(new AuthenticationTicket(null, properties)); } }
public IFOAuthContext(IOwinContext context, JObject user, IFOAuthAccess access) : this(context, user, access.AccessToken, access.RefreshToken, access.ExpiresIn) { }