CrazyIter_Bin 3 lat temu
rodzic
commit
2ab35fc9b3

+ 21 - 17
TEAMModelAPI/ApiTokenAttribute.cs

@@ -93,28 +93,32 @@ namespace TEAMModelOS.Filter
                 if (!string.IsNullOrWhiteSpace(authtoken) && JwtAuthExtension.ValidateApiToken(authtoken, _option.JwtSecretKey))
                 {
                     var jwt = new JwtSecurityTokenHandler().ReadJwtToken(authtoken);
-                    id = jwt.Payload.Sub;
-                    school = jwt.Payload.Azp;
-                    jti = jwt.Payload.Jti;
-                    var permissions = jwt.Claims.Where(c => c.Type.Equals("auth"));
-                    ///当前请求的api的设置的permission值是否包含在 从jwt的获取["1","2","3","4","5"]值中
-                    if (!string.IsNullOrWhiteSpace(_auth)&& permissions.Count()>0)
+                    string iss = jwt.Payload.Iss; //iss 检查jwt是否是测试站,正式站的授权key 
+                    if (iss.Equals(_option.HostName))
                     {
-                       if (permissions.Select(x=>x.Value).Contains(_auth))
-                       {
-                            pass = true;
-                       }
-                    }
+                        //aud  受众
+                        id = jwt.Payload.Sub;//主题,又是应用APP
+                        school = jwt.Payload.Azp;//学校编码
+                        jti = jwt.Payload.Jti;//jwt唯一标识
+                        var permissions = jwt.Claims.Where(c => c.Type.Equals("auth"));
+                        ///当前请求的api的设置的permission值是否包含在 从jwt的获取["1","2","3","4","5"]值中
+                        if (!string.IsNullOrWhiteSpace(_auth) && permissions.Count() > 0)
+                        {
+                            if (permissions.Select(x => x.Value).Contains(_auth))
+                            {
+                                pass = true;
+                            }
+                        }
 
-                    if (!string.IsNullOrEmpty(id) && !string.IsNullOrEmpty(school) && !string.IsNullOrEmpty(jti))
-                    {
-                        //AIP 开启限流策略 处理限流问题
-                        if (_limit)
+                        if (!string.IsNullOrEmpty(id) && !string.IsNullOrEmpty(school) && !string.IsNullOrEmpty(jti))
                         {
+                            //AIP 开启限流策略 处理限流问题
+                            if (_limit)
+                            {
+                            }
                         }
-                    } 
+                    }
                 }
-
                 if (pass)
                 {
                     context.HttpContext.Items.Add("ID", id);

+ 76 - 0
TEAMModelAPI/Controllers/School/SchoolController.cs

@@ -0,0 +1,76 @@
+using Microsoft.AspNetCore.Mvc;
+using System;
+using System.Collections.Generic;
+using System.Text;
+using System.Threading.Tasks;
+using TEAMModelOS.Models;
+using TEAMModelOS.SDK;
+using TEAMModelOS.SDK.DI;
+using System.Text.Json;
+using TEAMModelOS.SDK.Models;
+using TEAMModelOS.SDK.Extension;
+using Azure.Cosmos;
+using Microsoft.AspNetCore.Http;
+using Microsoft.Extensions.Options;
+using System.IO;
+using System.Dynamic;
+using System.Net.Http;
+using System.Net;
+using Newtonsoft.Json;
+using System.Linq;
+using StackExchange.Redis;
+using static TEAMModelOS.SDK.Models.Teacher;
+using Microsoft.Extensions.Configuration;
+using TEAMModelOS.Filter;
+using Microsoft.AspNetCore.Authorization;
+
+namespace TEAMModelAPI.Controllers
+{
+    [ProducesResponseType(StatusCodes.Status200OK)]
+    [ProducesResponseType(StatusCodes.Status400BadRequest)]
+    [ApiController]
+    [Route("school")]
+    public class SchoolController : ControllerBase
+    {
+        public AzureCosmosFactory _azureCosmos;
+        private readonly AzureStorageFactory _azureStorage;
+        private readonly AzureRedisFactory _azureRedis;
+        private readonly DingDing _dingDing;
+        private readonly Option _option;
+        int baseSpaceSize = 1; //學校保底空間大小(1G)
+        private readonly double bytes = 1073741824;
+        private readonly int redisAclassoneDbNum = 8; //AclassOne Redis DB號
+        private readonly IConfiguration _configuration;
+        public SchoolController(AzureCosmosFactory azureCosmos, AzureStorageFactory azureStorage, AzureRedisFactory azureRedis, DingDing dingDing, IOptionsSnapshot<Option> option, IConfiguration configuration)
+        {
+            _azureCosmos = azureCosmos;
+            _azureStorage = azureStorage;
+            _azureRedis = azureRedis;
+            _dingDing = dingDing;
+            _option = option?.Value;
+            _configuration = configuration;
+        }
+
+        /// <summary>
+        ///  学校信息
+        /// </summary>
+        /// <param name="request"></param>
+        /// <returns></returns>
+        [ProducesDefaultResponseType]
+        [HttpGet("get-school-info")]
+        [ApiToken(Auth = "1",Name = "学校信息", Limit=true)]
+        public async Task<IActionResult> GetSchoolInfo()
+        {
+            var (id, school) = HttpContext.GetApiTokenInfo();
+            School data = await _azureCosmos.GetCosmosClient().GetContainer(Constant.TEAMModelOS, "School").ReadItemAsync<School>(school, new PartitionKey("Base"));
+            List<dynamic> period = new List<dynamic>();
+            data.period.ForEach(x => { period.Add(new { x.subjects ,x.grades,x.name,x.id,x.campusId,x.semesters}); });
+            return Ok(new { 
+                id = data.id, name = data.name, data.areaId, type = data.type, 
+                data.region, data.province, data.city, data.dist,
+                campuses=data.campuses,
+                period
+            });
+        }
+    }
+}

+ 2 - 1
TEAMModelAPI/Startup.cs

@@ -1,4 +1,4 @@
-using Microsoft.AspNetCore.Builder;
+ using Microsoft.AspNetCore.Builder;
 using Microsoft.AspNetCore.Hosting;
 using Microsoft.AspNetCore.HttpsPolicy;
 using Microsoft.AspNetCore.Mvc;
@@ -80,6 +80,7 @@ namespace TEAMModelAPI
             services.AddAzureStorage(Configuration.GetValue<string>("Azure:Storage:ConnectionString"));
             services.AddAzureRedis(Configuration.GetValue<string>("Azure:Redis:ConnectionString"));
             services.AddAzureCosmos(Configuration.GetValue<string>("Azure:Cosmos:ConnectionString"));
+            services.AddAzureServiceBus(Configuration.GetValue<string>("Azure:ServiceBus:ConnectionString"));
             services.AddMemoryCache();
             services.AddSnowflakeId(Convert.ToInt64(Configuration.GetValue<string>("Option:LocationNum")), 1);
             services.AddHttpClient();

+ 38 - 3
TEAMModelAPI/appsettings.Development.json

@@ -12,7 +12,7 @@
     "LocationNum": "1",
     "HostName": "localhost:5001",
     "AllowedHosts": [ "localhost", "*.teammodel.cn", "*.teammodel.net", "*.habookaclass.biz", "test" ],
-    "Issuer": "localhost:5001",
+    "Issuer": "www.teammodel.cn",
     "JwtSecretKey": "fXO6ko/qyXeYrkecPeKdgXnuLXf9vMEtnBC9OB3s+aA=",
     "Exp": 86400,
     "IdTokenSalt": "8263692E2213497BB55E74792B7900B4",
@@ -26,12 +26,47 @@
       "ConnectionString": "AccountEndpoint=https://cdhabookdep-free.documents.azure.cn:443/;AccountKey=JTUVk92Gjsx17L0xqxn0X4wX2thDPMKiw4daeTyV1HzPb6JmBeHdtFY1MF1jdctW1ofgzqkDMFOtcqS46by31A==;"
     },
     "Redis": {
-      "ConnectionString": "106.12.23.251:6379,password=habook,ssl=false,abortConnect=False,writeBuffer=10240"
+      "ConnectionString": "52.130.252.100:6379,password=habook,ssl=false,abortConnect=False,writeBuffer=10240"
     },
     "ServiceBus": {
       "ConnectionString": "Endpoint=sb://teammodelos.servicebus.chinacloudapi.cn/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=Sy4h4EQ8zP+7w/lOLi1X3tGord/7ShFHimHs1vC50Dc=",
       "ActiveTask": "dep-active-task",
-      "NoticeTask": "dep-notice-task"
+      "ItemCondQueue": "dep-itemcond"
     }
+    //"Storage": {
+    //  "ConnectionString": "DefaultEndpointsProtocol=https;AccountName=teammodelos;AccountKey=Dl04mfZ9hE9cdPVO1UtqTUQYN/kz/dD/p1nGvSq4tUu/4WhiKcNRVdY9tbe8620nPXo/RaXxs+1F9sVrWRo0bg==;EndpointSuffix=core.chinacloudapi.cn"
+    //},
+    //"Cosmos": {
+    //  "ConnectionString": "AccountEndpoint=https://teammodelos.documents.azure.cn:443/;AccountKey=clF73GwPECfP1lKZTCvs8gLMMyCZig1HODFbhDUsarsAURO7TcOjVz6ZFfPqr1HzYrfjCXpMuVD5TlEG5bFGGg==;"
+    //},
+    //"Redis": {
+    //  "ConnectionString": "CoreRedisCN.redis.cache.chinacloudapi.cn:6380,password=LyJWP1ORJdv+poXWofAF97lhCEQPg1wXWqvtzXGXQuE=,ssl=True,abortConnect=False"
+    //},
+    //"ServiceBus": {
+    //  "ConnectionString": "Endpoint=sb://coreiotservicebuscnpro.servicebus.chinacloudapi.cn/;SharedAccessKeyName=TEAMModelOS;SharedAccessKey=llRPBMDJG9w1Nnifj+pGhV0g4H2REcq0PjvX2qqpcOg=",
+    //  "ActiveTask": "active-task",
+    //  "ItemCondQueue": "itemcond"
+    //}
+  },
+  "HaBookAuth": {
+    "CoreId": {
+      "userinfo": "https://api2.teammodel.cn/Oauth2/GetUserInfos"
+    },
+    "Account": "https://account.teammodel.cn",
+    "CoreAPI": "https://api2.teammodel.cn",
+    "CoreService": {
+      "clientID": "c7317f88-7cea-4e48-ac57-a16071f7b884",
+      "clientSecret": "kguxh:V.PLmxBdaI@jnrTrDSth]A3346",
+      "deviceinfo": "https://api2.teammodel.cn/oauth2/getdeviceinfos",
+      "sendnotification": "https://api2.teammodel.net/service/sendnotification",
+      "getnotification": "https://api2.teammodel.net/service/getnotification",
+      "delnotification": "https://api2.teammodel.net/service/delnotification"
+    }
+  },
+  "DingDingAuth": {
+    "Agentld": "1290158212",
+    "appKey": "dingrucgsnt8p13rfbgd",
+    "appSecret": "Gyx_N57yZslhQOAhAPlvmCwOp_qTm1DScKbd5OoOE0URAW4eViYA2Sk_ZxKb-8WG",
+    "getuserinfo_bycode": "https://oapi.dingtalk.com/sns/getuserinfo_bycode?accessKey=xxx&timestamp=xxx&signature=xxx"
   }
 }

+ 12 - 1
TEAMModelOS.SDK/Extension/HttpContextExtensions.cs

@@ -59,7 +59,18 @@ namespace TEAMModelOS.SDK.Extension
                 return null;
             }
         }
-
+        /// <summary>
+        /// 取得AuthToken權杖資訊
+        /// </summary>        
+        /// <param name="key">Key Name</param>
+        /// <returns></returns>
+        public static (string id, string school) GetApiTokenInfo(this HttpContext httpContext, string key = null)
+        {
+            object id = null,  school = null;
+            httpContext?.Items.TryGetValue("ID", out id);
+            httpContext?.Items.TryGetValue("School", out school);
+            return (id?.ToString(),  school?.ToString());
+        }
         /// <summary>
         /// 取得AuthToken權杖資訊
         /// </summary>