using IdentityServer4;
using IdentityServer4.Models;
using Long.Common;

namespace Auth
{
    /// <summary>
    /// 认证配置
    /// </summary>
    public static class IdentityServer4Config
    {
        /// <summary>
        /// 范围
        /// </summary>
        /// <returns></returns>
        public static IEnumerable<ApiScope> ApiScopes()
        {
            return new List<ApiScope>
            {
                new ApiScope(IdsScope.GuestApi, "GuestAPI"),
                new ApiScope(IdsScope.UserApi, "UserAPI"),
                new ApiScope(IdsScope.AdminApi, "AdminAPI")
            };
        }

        /// <summary>
        /// 资源
        /// </summary>
        /// <returns></returns>
        public static IEnumerable<ApiResource> ApiResources()
        {
            return new List<ApiResource>
            {
                new ApiResource("LongApi", "Dotnet Core API")
                {
                    Scopes = { IdsScope.GuestApi, IdsScope.UserApi, IdsScope.AdminApi }
                }
            };
        }

        /// <summary>
        /// 客户端
        /// </summary>
        /// <returns></returns>
        public static IEnumerable<Client> Clients()
        {
            var configurationBuilder = new ConfigurationBuilder();
            configurationBuilder.AddJsonFile("appsettings.json", false, true);
            var configuration = configurationBuilder.Build();
            return new List<Client>
            {
                new Client
                {
                    ClientId = "guest",
                    AllowedGrantTypes = GrantTypes.ClientCredentials,
                    ClientSecrets =
                    {
                        new Secret("Long".Sha256())
                    },
                    AllowedScopes =
                    {
                        IdsScope.GuestApi,
                        IdentityServerConstants.StandardScopes.OpenId,
                        IdentityServerConstants.StandardScopes.OfflineAccess,
                        IdentityServerConstants.StandardScopes.Profile
                    },
                    AccessTokenLifetime = 24 * 3600,
                    AllowOfflineAccess = true,
                    RefreshTokenExpiration = TokenExpiration.Sliding,
                    SlidingRefreshTokenLifetime = 24 * 3600
                },
                new Client
                {
                    ClientId = "user",
                    AllowedGrantTypes = GrantTypes.ResourceOwnerPassword,
                    ClientSecrets =
                    {
                        new Secret("Long".Sha256())
                    },
                    AllowedScopes =
                    {
                        IdsScope.UserApi,
                        IdentityServerConstants.StandardScopes.OpenId,
                        IdentityServerConstants.StandardScopes.OfflineAccess,
                        IdentityServerConstants.StandardScopes.Profile
                    },
                    AccessTokenLifetime = 24 * 3600,
                    AllowOfflineAccess = true,
                    RefreshTokenExpiration = TokenExpiration.Sliding,
                    SlidingRefreshTokenLifetime = 24 * 3600
                },
                new Client
                {
                    ClientId = "admin",
                    AllowedGrantTypes = GrantTypes.ResourceOwnerPassword,
                    ClientSecrets =
                    {
                        new Secret("Long".Sha256())
                    },
                    AllowedScopes =
                    {
                        IdsScope.AdminApi,
                        IdentityServerConstants.StandardScopes.OpenId,
                        IdentityServerConstants.StandardScopes.OfflineAccess,
                        IdentityServerConstants.StandardScopes.Profile
                    },
                    AccessTokenLifetime = 24 * 3600,
                    AllowOfflineAccess = true,
                    RefreshTokenExpiration = TokenExpiration.Sliding,
                    SlidingRefreshTokenLifetime = 24 * 3600
                },
                new Client
                {
                    ClientId = "web",
                    //ClientName = "admin",
                    AllowedGrantTypes = GrantTypes.Implicit,
                    RequireConsent = false,
                    RedirectUris = {configuration["RedirectUris"] },
                    PostLogoutRedirectUris = {configuration["PostLogoutRedirectUris"] },
                    ClientSecrets =
                    {
                        new Secret("Long".Sha256())
                    },
                    AllowedScopes =
                    {
                        IdentityServerConstants.StandardScopes.OpenId,
                        IdentityServerConstants.StandardScopes.Profile
                    }
                }
            };
        }
    }
}