void EnsureAccessGrantValid(ref AccessGrant accessGrant) { if (accessGrant != null && ShouldRefreshToken(accessGrant)) { accessGrant = RefreshToken(accessGrant); } }
/// <summary> /// 应用授权 /// </summary> /// <returns></returns> public static AccessGrant AppAuthorize() { EnsureInit(); log.Info("app authorize begin"); AccessGrant accessGrant = OAuthProvider.AppAuthorize(); log.InfoFormat("app authorize success. access token: {0}", ag.AccessToken); return(accessGrant); }
public static string Serialize(AccessGrant accessGrant) { var buffer = new byte[48]; AppendBytes(buffer, 0, accessGrant.AccessToken); AppendBytes(buffer, 16, accessGrant.RefreshToken); AppendBytes(buffer, 32, accessGrant.CreateTime); AppendBytes(buffer, 36, accessGrant.ExpireTime); AppendBytes(buffer, 40, accessGrant.ClientId); AppendBytes(buffer, 44, BitConverter.GetBytes(accessGrant.UserId)); return(Convert.ToBase64String(buffer)); }
/// <summary> /// 刷新 AccessToken /// </summary> public virtual AccessGrant RefreshToken(AccessGrant accessGrant) { RefreshTokenData tokenData = CreateRefreshTokenTokenData(accessGrant); var result = OAuthOperator.GetToken(tokenData); if (result.Code == 0) { var ag = result.Data; if (ag.UserId == 0) { OnAppAuthorizeSuccess(ag); } else { OnUserAuthorizeSuccess(ag); } return(ag); } throw new OAuthException(result.Code + ":" + result.Message); }
protected override void OnUserAuthorizeSuccess(AccessGrant accessGrant) { Arguments.NotNull(accessGrant, "accessGrant"); var context = HttpContext.Current; if (context.IsAvailable()) { var ticketExpireTime = NetworkTime.Now.AddDays(TimeoutDays); var formsIdentity = context.User.Identity as FormsIdentity; if (formsIdentity != null) { //续约 ticketExpireTime = formsIdentity.Ticket.Expiration; } var ticket = CreateFormsAuthenticationTicket(accessGrant, ticketExpireTime); SetAuthCookie(ticket); context.User = new GenericPrincipal(new FormsIdentity(ticket), new string[0]); context.Items[ContextAccessGrantKey] = accessGrant; } }
protected override void OnUserAuthorizeSuccess(AccessGrant accessGrant) { }
protected virtual void OnAppAuthorizeSuccess(AccessGrant accessGrant) { appAccessGrant = accessGrant; }
/// <summary> /// 当成功授权时处理 /// </summary> protected abstract void OnUserAuthorizeSuccess(AccessGrant accessGrant);
/// <summary> /// 创建 RefreshToken 的请求对象 /// </summary> protected virtual RefreshTokenData CreateRefreshTokenTokenData(AccessGrant accessGrant) { return(new RefreshTokenData(OAuthContext.ClientId, OAuthContext.ClientSecret, accessGrant.RefreshToken)); }
/// <summary> /// 判断是否应该刷新 AccessToken /// </summary> protected virtual bool ShouldRefreshToken(AccessGrant acessGrant) { return(!acessGrant.IsEffective() || (NetworkTime.Now - acessGrant.CreateTime).TotalSeconds / (acessGrant.ExpireTime - acessGrant.CreateTime).TotalSeconds > 0.75); }
internal OAuthContext(AccessGrant appAccessGrant, AccessGrant userAccessGrant) { this.appAccessGrant = appAccessGrant; this.userAccessGrant = userAccessGrant; }
protected virtual FormsAuthenticationTicket CreateFormsAuthenticationTicket(AccessGrant accessGrant, DateTime expireTime) { return(new FormsAuthenticationTicket(1, accessGrant.UserId.ToString(), NetworkTime.Now, expireTime, true, AccessGrantSerializer.Serialize(accessGrant))); }