Esempio n. 1
0
        public virtual void Login()
        {
            if (ctx.viewer.IsLogin)
            {
                echoError("对不起,您已经登录");
                return;
            }

            // 1) 登录的第三方类型
            String connectType = ctx.Get("connectType");

            AuthConnect connect = AuthConnectFactory.GetConnect(connectType);

            if (connect == null)
            {
                echoError("连接类型不存在:" + connectType);
                return;
            }

            // 2) 根据类型,redirect到第三方授权。回调网址callback 是 ProcessLogin()
            ctx.web.SessionSet("__connectType", connect.GetType().FullName);
            ctx.web.SessionSet("__connectFrom", "login");

            redirectUrl(connect.GetAuthorizationFullUrl());
        }
Esempio n. 2
0
        private void processBind(AuthConnect connect, String code)
        {
            if (ctx.viewer.IsLogin == false)
            {
                echoError("对不起,请先登录");
                return;
            }

            // 检查是否绑定过
            if (connectService.HasBind(ctx.viewer.Id, connect.GetType().FullName))
            {
                echoError("对不起,已经绑定");
                return;
            }

            // 获取用户 uid
            AccessToken x = OAuthClient.New().GetAccessToken(connect, code, connect.HttpMethod_AccessToken);

            x.Uid = connect.GetUid(x);

            // 获取用户名称
            OAuthUserProfile userProfile = null;

            try {
                userProfile = connect.GetUserProfile(x);
            }
            catch (HttpClientException ex) {
                if (ex.Message.IndexOf("applications over the unaudited use restrictions") > 0)
                {
                    echo(getTestRestrictionsMsg("绑定"));
                    return;
                }
                else
                {
                    throw ex;
                }
            }

            x.Name = userProfile.Name;

            Result result = connectService.Create(ctx.viewer.obj as User, connect.GetType().FullName, x);

            // 日志
            logger.Info("accessToken=" + x.Token);
            logger.Info("uid=" + x.Uid);
            logger.Info("refresh_token=" + x.RefreshToken);
            logger.Info("expires_in=" + x.ExpiresIn);
            logger.Info("scope=" + x.Scope);

            if (result.HasErrors)
            {
                echo(result.ErrorsHtml);
            }
            else
            {
                echoRedirect(lang("opok"), "/");
            }
        }
Esempio n. 3
0
        private void loadUserProfile(AuthConnect connect, AccessToken accessToken)
        {
            OAuthUserProfile user = connect.GetUserProfile(accessToken);

            ctx.SetItem("__currentOAuthUser", user);
            ctx.SetItem("__currentAccessToken", accessToken);

            content(loadHtml(confirmUserInfo));
        }
Esempio n. 4
0
        private void processLogin(AuthConnect connect, String code)
        {
            if (ctx.viewer.IsLogin)
            {
                echoError("对不起,您已经登录");
                return;
            }

            AccessToken accessToken = OAuthClient.New().GetAccessToken(connect, code, connect.HttpMethod_AccessToken);

            logger.Info("accessToken=" + accessToken.Token);
            logger.Info("uid=" + accessToken.Uid);
            logger.Info("refresh_token=" + accessToken.RefreshToken);
            logger.Info("expires_in=" + accessToken.ExpiresIn);
            logger.Info("scope=" + accessToken.Scope);

            String uid = connect.GetUid(accessToken);

            // 1) 检查网站中是否有此用户
            UserConnect x = connectService.GetConnectInfo(uid, connect.GetType().FullName);

            // 第一次登录
            if (x == null)
            {
                try {
                    loadUserProfile(connect, accessToken);
                }
                catch (HttpClientException ex) {
                    if (ex.Message.IndexOf("applications over the unaudited use restrictions") > 0)
                    {
                        echo(getTestRestrictionsMsg("登录"));
                    }
                    else
                    {
                        throw ex;
                    }
                }
            }
            // 其他:获取用户信息,然后登录
            else
            {
                checkAccessToken(x, accessToken);

                LoginTime expiration = LoginTime.OneWeek;
                loginService.Login(x.User, x.Id, expiration, ctx.Ip, ctx);

                echoRedirect("登录成功", "/");
            }
        }
Esempio n. 5
0
        public virtual void Callback()
        {
            string code = ctx.Get("code");

            if (strUtil.IsNullOrEmpty(code))
            {
                echoError("code无效");
                return;
            }

            Object connectType = ctx.web.SessionGet("__connectType");

            if (connectType == null)
            {
                echoError("无效的 connect type");
                return;
            }

            AuthConnect connect = AuthConnectFactory.GetConnect(connectType.ToString());

            if (connect == null)
            {
                echoError("此连接类型不存在:" + connectType);
                return;
            }

            Object connectFrom = ctx.web.SessionGet("__connectFrom");

            if (connectFrom == null)
            {
                echoError("进入本页方式错误");
            }
            else if (connectFrom.ToString() == "bind")
            {
                processBind(connect, code);
            }
            else if (connectFrom.ToString() == "login")
            {
                processLogin(connect, code);
            }
            else
            {
                echoError("进入本页方式错误");
            }
        }
Esempio n. 6
0
        public void testLoadConfig()
        {
            List <AuthConnectConfig> cfgList = cdb.findAll <AuthConnectConfig>();

            Assert.IsTrue(cfgList.Count > 0);

            Dictionary <string, AuthConnect> connects = AuthConnectFactory.GetAllConnects();

            Assert.IsNotNull(connects);
            Assert.IsTrue(connects.Count > 0);

            String      qqConnect   = typeof(QQConnect).FullName;
            AuthConnect authConnect = null;

            connects.TryGetValue(qqConnect, out authConnect);

            Assert.IsNotNull(authConnect);

            AuthConnect objConnect = AuthConnectFactory.GetConnect(qqConnect);

            Assert.IsNotNull(objConnect);
        }
Esempio n. 7
0
        public virtual void Bind()
        {
            String connectType = ctx.Get("connectType");

            AuthConnect connect = AuthConnectFactory.GetConnect(connectType);

            if (connect == null)
            {
                echoError("此连接类型不存在:" + connectType);
                return;
            }

            // 检查是否绑定过
            if (connectService.HasBind(ctx.viewer.Id, connect.GetType().FullName))
            {
                echoError("对不起,已经绑定");
                return;
            }

            ctx.web.SessionSet("__connectType", connect.GetType().FullName);
            ctx.web.SessionSet("__connectFrom", "bind");

            redirectUrl(connect.GetAuthorizationFullUrl());
        }
Esempio n. 8
0
        public virtual void SaveFirstLogin()
        {
            if (ctx.viewer.IsLogin)
            {
                echoError("对不起,您已经登录");
                return;
            }

            Object connectType = ctx.web.SessionGet("__connectType");

            if (connectType == null)
            {
                echoError("无效的 connect type");
                return;
            }

            AuthConnect connect = AuthConnectFactory.GetConnect(connectType.ToString());

            if (connect == null)
            {
                echoError("此连接类型不存在:" + connectType);
                return;
            }

            AccessToken accessToken = getAccessToken();

            OAuthUserProfile userProfile = connect.GetUserProfile(accessToken);

            if (userProfile == null)
            {
                echoError("无法获取正常 user profile");
                return;
            }

            accessToken.Name = userProfile.Name;

            // 注册用户
            User user = new User();

            user.Name = ctx.Post("userName");
            user.Url  = ctx.Post("userUrl");

            Result result = userService.RegisterNoPwd(user);

            if (result.HasErrors)
            {
                echoError(result);
                return;
            }

            result = AvatarUploader.SaveRemote(userProfile.PicUrlBig, user.Id);
            if (result.IsValid)
            {
                user.Pic = result.Info.ToString();
                user.update();
            }
            else
            {
                echoError(result);
                return;
            }

            // 是否开启空间
            RegHelper.CheckUserSpace(user, ctx);

            // 绑定用户
            Result saveResult = connectService.Create(user, connect.GetType().FullName, accessToken);

            if (saveResult.IsValid)
            {
                UserConnect userConnect = saveResult.Info as UserConnect;
                loginService.Login(user, userConnect.Id, LoginTime.OneWeek, ctx.Ip, ctx);   // 登录
                echoRedirect("登录成功", "/");
            }
            else
            {
                echoError(saveResult);
            }
        }