예제 #1
0
        }                                                             //injected by IOC


        public DataModels.TokenInfo Get(TokenInfoRequest TokenInfoRequest)
        {
            if (string.IsNullOrWhiteSpace(TokenInfoRequest.access_token))
            {
                throw new DataModels.TokenRequestError(DataModels.ErrorCodes.invalid_request, "Missing access token");
            }

            DataModels.TokenInfo info = TokenModel.GetToken <DataModels.TokenInfo>(TokenInfoRequest.access_token);

            if (info == null)
            {
                Response.StatusCode = (int)System.Net.HttpStatusCode.NotFound;
                return(null);
            }


            Response.StatusCode = (int)System.Net.HttpStatusCode.Found;
            if (TokenInfoRequest.validate_only)
            {
                return(null);
            }

            if (!string.IsNullOrWhiteSpace(info.resource_owner_id))
            {
                info.owner = ResourceOwnerModel.GetByID(info.resource_owner_id);
            }

            return(info);

            Response.Write(info.ToJson());
            Response.Close();
            return(null);

            return(info);
        }
예제 #2
0
        public void RequestFilter(IHttpRequest req, IHttpResponse res, object requestDto)
        {
            string auth      = req.Headers.Get("Authorization");
            bool   validUser = false;

            if (!string.IsNullOrWhiteSpace(auth))
            {
                Match rawToken = MATCH_TOKEN.Match(auth);

                if (rawToken.Success && rawToken.Groups["token_type"].Success && rawToken.Groups["token"].Success)
                {
                    DataModels.Token token = TokenModel.GetToken <DataModels.Token>(rawToken.Groups["token"].Value);
                    req.Items.Add("auth:rawtoken", rawToken);

                    if (SetToken)
                    {
                        req.Items.Add("auth:token", token);
                    }

                    if (SetClient)
                    {
                        req.Items.Add("auth:client", ClientModel.GetClientByID(token.client_id));
                    }

                    if (!string.IsNullOrWhiteSpace(token.resource_owner_id) && SetUser)
                    {
                        DataModels.ResourceOwner owner = ResourceOwnerModel.GetByID(token.resource_owner_id);
                        if (owner != null)
                        {
                            req.Items.Add("auth:user", owner);
                            validUser = true;
                        }
                    }
                }
            }


            if (RequireValidUser && !validUser)
            {
                res.StatusCode        = (int)System.Net.HttpStatusCode.Unauthorized;
                res.StatusDescription = "Valid bearer token required";
                res.AddHeader("WWW-Authenticate", "OAuth2 realm=\"{0}\"".Fmt(req.GetApplicationUrl()));
                res.Close();
            }
        }