session/session.proto

syntax = "proto3";

package jsgenerate_kb2022.session;
option go_package = "gateway/protocol/session";

import "google/api/annotations.proto";

// 用戶會話相關接口
service Session {
    // 用戶登入
    rpc Sigin (SiginRequest) returns (SiginResponse){
        option (google.api.http) = {
            post: "/api/v1/session"
            body: "*"
        };
    }
    // 用戶登入和 Sigin 類似,但這個接口要求用戶輸入一個驗證碼用以過濾機器人登入
    rpc Captcha (CaptchaRequest) returns (SiginResponse){
        option (google.api.http) = {
            post: "/api/v1/session/captcha"
            body: "*"
        };
    }
    // 用戶登出
    rpc Signout (SignoutRequest) returns (SignoutResponse){
        option (google.api.http) = {
            delete: "/api/v1/session"
        };
    }
    // 返回登入的用戶信息
    rpc Get (GetRequest) returns (GetResponse){
        option (google.api.http) = {
            get: "/api/v1/session"
        };
    }
    // 返回與 token 關聯的 用戶信息
    rpc GetByAccess (GetByAccessRequest) returns (GetResponse){
        option (google.api.http) = {
            get: "/api/v1/session/{access}"
        };
    }
    // 刷新 token
    rpc Refresh (RefreshRequest) returns (RefreshResponse){
        option (google.api.http) = {
            post: "/api/v1/session/refresh"
            body: "*"
        };
    }

    // 只有 root 可調用,創建一個訪問內部接口的憑證。
    rpc Chains (ChainsRequest) returns (ChainsResponse){
        option (google.api.http) = {
            post: "/api/v1/session/chains"
            body: "*"
        };
    }
}
message Data{
    // 用戶唯一識別代碼
    int64 id = 1;
    // 用戶名
    string name = 2;
    // 用戶昵稱
    string nickname = 3;
    // 綁定電話
    string phone = 4;
    // 用戶頭像
    string avatar = 5;
    // 用戶所在組
    repeated int32 groups = 6;
    // 用戶擁有權限
    repeated int32 permissions = 7;
    // 用戶管理區域
    repeated string areas = 8;
    // 用戶優先級,可設置任務 優先級 只能小於等於此值
    int32 priority = 9;
    // 用戶等級,可設置任務 等級 只能小於等於此值
    int32 level = 10;
}
message SiginRequest{
    // 登入平臺,每種平臺 同時只能有一個 合法的 session,後續登入將使更早登入的 session 失效,推薦值如下
    // * web
    // * ios
    // * android
    // * fuchsia
    // * chrome os
    // * windows
    // * linux
    // * mac
    // * microapp of wechat  ->  微信小程式
    // * microapp of bytedance  -> 抖音小程式
    // * microapp of baidu -> 百度小程式
    // 不同的客戶端應該使用合適的值,以使服務器可以正確跟蹤 客戶端版本信息
    string platform = 1;
    // 登入用的 用戶名稱 或者 綁定電話
    string name = 2;
    // 密碼使用的加密方式
    // * none
    // * md5
    // * sha1
    // * sha224
    // * sha256
    // * sha384
    // * sha512
    // none 方式僅用於方便測試請勿在生產中使用
    string encrypt = 3;
    // 執行請求時的時間 unix 誤差浮動不能超過 5 分鐘
    int64 at = 4;
    // 登入密碼
    // var password=input_password
    // if encrypt != none {
    //      var at = at.toString()
    //      password = md5(password+".kb2022").lowerHex()
    //      password = encrypt(at+password).lowerHex()
    // }
    string password = 5;

    // 是否要返回 SiginResponse.data
    bool data = 6;
    // 是否將返回的令牌設置到 cookie 僅用於測試時使用
    bool cookie = 7;
}
message CaptchaRequest{
    // 登入平臺,每種平臺 同時只能有一個 合法的 session,後續登入將使更早登入的 session 失效,推薦值如下
    // * web
    // * ios
    // * android
    // * fuchsia
    // * chrome os
    // * windows
    // * linux
    // * mac
    // * microapp of wechat  ->  微信小程式
    // * microapp of bytedance  -> 抖音小程式
    // * microapp of baidu -> 百度小程式
    // 不同的客戶端應該使用合適的值,以使服務器可以正確跟蹤 客戶端版本信息
    string platform = 1;
    // 登入用的 用戶名稱 或者 綁定電話
    string name = 2;
    // 密碼使用的加密方式
    // * none
    // * md5
    // * sha1
    // * sha224
    // * sha256
    // * sha384
    // * sha512
    // none 方式僅用於方便測試請勿在生產中使用
    string encrypt = 3;
    // 執行請求時的時間 unix 誤差浮動不能超過 5 分鐘
    int64 at = 4;
    // 登入密碼
    // var password=input_password
    // if encrypt != none {
    //      var at = at.toString()
    //      password = md5(password+".kb2022").lowerHex()
    //      password = encrypt(at+password).lowerHex()
    // }
    string password = 5;

    // 是否要返回 SiginResponse.data
    bool data = 6;
    // 是否將返回的令牌設置到 cookie 僅用於測試時使用
    bool cookie = 7;

    // 驗證碼 id
    string captcha = 8;
    // 用戶輸入的驗證碼
    string answer = 9;
}
message Token {
    // 訪問 token
    // token 編碼規則爲爲 
    // var playdata=RawUrlBase64(id)+'.'+RawUrlBase64(platform)+'.'+RawUrlBase64(random_uuid)
    // var access=playdata + '.' + sign(playdata)
    string access = 1;
    // 刷新 token
    string refresh = 2;
    // 訪問 token 過期時間 unix
    int64 accessDeadline = 3;
    // 刷新 token 過期時間 unix
    int64 refreshDeadline = 4;
    // 會話最長維持時間 unix 如果爲 0 不限制
    int64 deadline = 5;
}
message SiginResponse{
    Token token = 1;
    Data data = 2;
}
message SignoutRequest{
}
message SignoutResponse{
}

// redis 序列化 session
message RawData{
    Token token = 1;
    Data data = 2;
}
message GetRequest{
}
message GetByAccessRequest{
    string access = 1;
}
message GetResponse{
    Data data = 1;
}
message RefreshRequest{
    string access = 1;
    string refresh = 2;
    // 如果爲 true 則將 新的 訪問 token 設置到 cookie 僅用於測試,勿在生產中使用
    bool cookie = 3;
}
message RefreshResponse{
    Token token = 1;
}

message ChainsRequest{
    // api 調用鏈,記錄了此調用在系統中走過的路由
    repeated string chains = 1;
    // 憑證有效秒數
    uint32 seconds = 2;
    // 是否將返回的令牌設置到 cookie 僅用於測試時使用
    bool cookie = 3;
}
message ChainsResponse{
    string access = 1;
}
message RawChains  {
    repeated string chains = 1;
	int64 deadline = 2;
}