小编And*_*zo1的帖子

如何在内存反应中正确存储访问令牌

我正在寻找一种干净的方法来在 React 中存储我的访问令牌。

  • 第一件事:我不想使用本地存储。我不需要使我的访问令牌持久化,因为我可以随时刷新它。
  • 我还排除了 cookie,因为我想防止 CSRF 攻击。通过仅将访问令牌存储在内存中,实际上需要加载页面才能获取令牌并验证请求(刷新令牌只能用于刷新)
  • 我想过使用 redux/context,但是,调用 API 的函数不是组件的子组件,因此我无法从中访问令牌。此外,我不想将令牌作为参数传递,因为我想保持 HTTP 逻辑的解耦。也许有一种干净的方法来使用它?

经过一番研究,我发现使用全局变量是获得我想要的东西的有效“欺骗”。但是,我猜测是否有更清晰的方法来获得相同的结果。

//token.js

const access_token= "";

export const setAccessToken(token){
 access_token=token;
}

export const getAccessToken(){
 return access_token;
}

//NOTICE:
// -ON LOGIN/REFRESH: I set the access token
// -ON API CALLS: I get the access token and I add it to the header
Run Code Online (Sandbox Code Playgroud)
api.js

const baseURL= "http://my_base_url";

const generateApiInterface = ()=>{
    let headers : any= {
       
    };
    
    token=getAccessToken(); //HERE I NEED TO RETRIEVE MY ACCESS TOKEN

    if(token){
        headers={ …
Run Code Online (Sandbox Code Playgroud)

javascript typescript reactjs

9
推荐指数
1
解决办法
9896
查看次数

将 firestore 文档转换为 flutter 类

我有一个名为“ Consultant我的应用程序从用户那里收集数据”的类。

class Consultant {
  final int id;
  final String consultantFirstName;
  final String consultantLastName;
  final String consultantNickName;
  final String consultantImageProfile;
  final String consultantCategory;
  final String consultantDescription;
  final String consultantLikes;
  final String consultantReviews;
  final String consultantStars;
  final String consultantPrice;
  final String consultantExperience;


  const Consultant({
    this.id,
    this.consultantFirstName,
    this.consultantLastName,
    this.consultantNickName,
    this.consultantImageProfile,
    this.consultantCategory,
    this.consultantDescription,
    this.consultantLikes,
    this.consultantReviews,
    this.consultantStars,
    this.consultantPrice,
    this.consultantExperience,
  });
}
Run Code Online (Sandbox Code Playgroud)

下面是我的一位用户的示例:

final Consultant marco = Consultant(
    id: 1,
    consultantFirstName: 'Marco',
    consultantLastName: 'Marcello',
    consultantNickName: 'Tarot and Dreams',
    consultantCategory: 'Tarocchi',
    consultantDescription: 'Ciao a tutti …
Run Code Online (Sandbox Code Playgroud)

firebase flutter google-cloud-firestore

6
推荐指数
1
解决办法
3097
查看次数

将 JWK json 转换为公钥 golang (lestrrat-go)

我使用 JWKS 格式从身份验证服务提供公钥,该公钥可用于验证来自该身份验证服务的令牌。但是,要执行验证,我需要从 JWK 重建公钥。我该如何转换它?

type JWKeys struct {
    Keys []JWKey `json:"keys"`
}

type JWKey struct {
    Kty string `json:"kty"`
    Use string `json:"use,omitempty"`
    Kid string `json:"kid,omitempty"`
    Alg string `json:"alg,omitempty"`

    Crv string `json:"crv,omitempty"`
    X   string `json:"x,omitempty"`
    Y   string `json:"y,omitempty"`
    D   string `json:"d,omitempty"`
    N   string `json:"n,omitempty"`
    E   string `json:"e,omitempty"`
    K   string `json:"k,omitempty"`
}

var PublicKey *rsa.PublicKey

func SetUpExternalAuth() {
    res, err := http.Get("my_url")

    if err != nil {
        log.Fatal("Can't retrieve the key for authentication")
    }

    bodyBytes, err := ioutil.ReadAll(res.Body)
    if err != …
Run Code Online (Sandbox Code Playgroud)

cryptography go pem public-key jwk

6
推荐指数
2
解决办法
1万
查看次数

构建安全 JWT 身份验证流程的指南?

最近我需要构建一个简单的 REST API,我阅读了关于最佳实践的不同文章,以尽可能减少我的 Web 应用程序的漏洞。在网上搜索,我找到了关于如何实现 JWT 令牌的不同教程,每个教程在某些方面都不同,我找不到一种节流良好的“通用方法”。最后,我实现了对我来说似乎最合理的解决方案,但我想确认这是处理此类身份验证的最有效方法。

在开始之前:

  • 注意 CORS 策略已实施
  • 为了签署令牌,我使用了“标准”库
  • 客户端和服务器之间的通信是加密的 (HTTPS)

#STEP 1:认证后生成令牌:

  1. 我生成了一个短期访问令牌(15 分钟)和一个用 HS256 算法签名的“长期”刷新令牌(7 天)(使用的密钥长度:512 位)
  2. 此外,我将刷新令牌的 ID 存储在缓存中。
  3. 我在响应中发回访问令牌(在客户端我只将它保存在内存中 [Redux]),同时我在 HTTPOnly cookie 中设置刷新令牌以使 XSS 攻击更加困难(并非不可能,我知道:目标是避免/减少窃取 cookie 的可能性)

#STEP 2:授权请求

为了授权请求,我只使用在请求标头中的字段中发送的访问令牌。仅当令牌有效时才允许请求。

#STEP 3:刷新访问令牌

为了刷新令牌,我向服务器发送刷新请求。服务器:

  1. 检查刷新令牌签名是否有效
  2. 检查刷新令牌是否在缓存中(检查是否过期并针对伪造令牌设置另一层防御)
  3. 生成新的访问令牌和新的刷新令牌,同时撤销先前的刷新令牌(并相应地更新缓存)
  4. 发送回响应中的访问令牌和仅 HTTP cookie 中的刷新令牌

有关刷新过程的更多信息

刷新被称为:

  1. 在访问令牌到期之前
  2. 在用户打开/刷新 Web 应用程序的确切时刻。这样,如果刷新令牌仍然有效,用户就会自动通过身份验证并准备好使用应用程序。注意刷新令牌只能用于刷新访问令牌,不能执行其他请求(为了避免CSRF攻击,cookie不能用于对请求进行身份验证,但用户需要加载网站才能获得访问令牌)

进一步的预防措施:关键操作

刷新令牌有两个过期时间。第一个,对于非关键操作,每次发布新令牌时都会刷新。这样,如果用户继续使用该应用程序,他/她可能会永远保持登录状态。对于关键操作,第二个(持续 3 小时)是“绝对的”。这意味着,在每次刷新时,关键操作的“计时器”不会刷新。

//To make it simpler:
nextToken.criticalExpiration=previousToken.criticalExpiration
Run Code Online (Sandbox Code Playgroud)

在“关键”计时器到期后调用刷新请求时,生成的访问令牌有一个字段为 false,表示执行关键操作的选项。如果为 false,则不允许进行关键操作(因此用户必须重新进行身份验证才能执行这些请求)

概括:

我想了解此过程是否正确执行或是否会产生漏洞。我知道在应用程序的其他部分和/或外部库中总是可能存在漏洞,但是,我想尽量减少留下可以被利用的东西的可能性。

security jwt reactjs redux

5
推荐指数
1
解决办法
212
查看次数

指定axios响应数据类型

data我正在构建 API 以将我的 React 应用程序与后端服务连接起来,并且我想使用 TypeScript 来指定Axios 请求内部的类型。如何在不修改其他字段的情况下更新 Axios 响应中的数据类型(请参阅下面代码中的 getAllProjects)?

class MyApi {
   constructor(token: string | null) {
     let headers: any = {
            'Content-Type': 'application/json',
     };
     if (token) {
        headers = {
          ...headers, //append other basic proprieties
          'Authorization': 'Token ' + token
        }
     }

     this.baseEndpoint = axios.create({
        baseURL: `${baseURL}`,
        headers: headers
     });

  }

  //DATA
  const getAllProjects = async () : Promise<AxiosResponse<?????>> => this.baseEndpoint.get('/projects/');
}

Run Code Online (Sandbox Code Playgroud)

简单地分配所需的类型(假设data: string[]本例)会引发以下错误:

Argument of type 'string[]' is not assignable to parameter of …
Run Code Online (Sandbox Code Playgroud)

typescript reactjs axios

4
推荐指数
1
解决办法
2万
查看次数

SQLMock 和 Gorm:模拟 Postgres 插入

当我尝试使用 SQLMock 和 Gorm.io 模拟 Postgres 插入时,我收到一条错误,指出该查询不是预期的。我尝试使用regexp.QuoteMeta()换行和转义字符串,但它不起作用。我添加并删除了 args 和 result,但错误仍然出现

如何通过 SQLMock 设置预期的查询?

我给你原始的 PostgresQuery 和 UserModel

//RAW QUERY
INSERT INTO "users" ("id","name","surname","birthdate","company","custom_claims","deleted") VALUES ($1,$2,$3,$4,$5,$6,$7)' with args [{Name: Ordinal:1 Value:my_user_id} {Name: Ordinal:2 Value:<nil>} {Name: Ordinal:3 Value:<nil>} {Name: Ordinal:4 Value:<nil>} {Name: Ordinal:5 Value:<nil>} {Name: Ordinal:6 Value:<nil>} {Name: Ordinal:7 Value:<nil>}]
Run Code Online (Sandbox Code Playgroud)
//Gorm model
type User struct {
    ID           string `gorm:"primaryKey"`
    Name         *string
    Surname      *string
    Birthdate    *time.Time
    Company      *string
    CustomClaims *json.RawMessage
    Deleted      gorm.DeletedAt
}

func (repository Repository) CreateUser(user users.User) (*users.User, error) { …
Run Code Online (Sandbox Code Playgroud)

go go-gorm go-sqlmock

2
推荐指数
1
解决办法
3377
查看次数

Apex 图表禁用滚动/缩放

我已经使用ApexCharts库构建了一个图表,并且我想禁用拖动图表的可能性。我应该如何更新我的配置(见下文)?我在文档中找不到任何详细信息。这是我的图表:

//My component
<Chart
  options={performanceLineChart.options}
  series={performanceLineChart.series}
/>

//My configurations
export const performanceLineChart = {
  options: {
    chart: {
      id: 'my-chart',
      type: 'line' as 'line',
      toolbar: {
        show: false //Disable toolbar
      }
    },
    stroke: {
      width: 5,
      curve: 'smooth' as 'smooth'
    },
    markers: {
      size: 4,
      hover: {
        size: undefined,
        sizeOffset: 2
      }
    },
    xasis: {
      type: 'numeric',
    }
  },
  series: [{
    name: 'Line 1',
    data: [
      [3066, 323],
      [6317, 312],
      [12498, 313],
      [24091, 326]
    ]
  },{
    name: 'Line …
Run Code Online (Sandbox Code Playgroud)

javascript typescript reactjs apexcharts

0
推荐指数
1
解决办法
8335
查看次数