GO中的用户认证系统

huangapple go评论77阅读模式
英文:

User Authentication System in GO

问题

在Go语言中是否存在现成的用户认证系统?

我来自Ruby背景,我希望能够找到类似于Ruby on Rails的device的东西,它是一个完整的用户管理系统。它可以处理从创建表单到密码哈希和加盐的所有事情。它还可以处理将数据存储到数据库中、设置会话等等。

我发现有一个叫做goth的包,可以实现社交登录(Facebook、Twitter等),但它不处理用户凭证的存储。它也不支持使用电子邮件+密码进行“普通”注册。

在Go语言中,我似乎只能找到一些关于如何制作基本且非常不安全的用户登录系统的教程。

我真的很希望能够找到一个既能处理用户认证又能通过OAuth/OAuth2进行社交登录的系统。是否存在这样的解决方案?

我想要的解决方案很可能是Go的一个Web框架的一部分,或者是一个插件。

背景信息:
考虑到我的Rails应用程序使用了太多的内存,我正在考虑将其迁移到一个性能更高的框架上。因此,我开始关注Go语言。然而,如果没有一个可靠的用户认证系统,我就无法转换到Go。自己制作用户认证系统并不是一个很好的做法。

英文:

Does a pre-made user authentication system exist in Go?

I am coming from a Ruby background, and I would really like to see something similar to Ruby on Rails' device which is a complete user management system. It takes care of everything from creating forms to hashing and salting passwords. It also takes care of storing data to the database, setting up sessions and more.

I have found that there is package called goth which allows for social logins (facebook, twitter ++) but it does not handle storage of the user credentials. It also does not allow for "normal" signup with email + password.

All I can seem to find on this topic for Go is some tutorials on how to make your own basic, very unsafe, user login system.

I would really love to see a system which takes care of the user auth as well as allows for social login via OAuth/OAuth2. Does anything like this exist?

The solution I am after would most likely be part of, or a plugin for, a Go web framework.

Background

I am considering moving my Rails app over to a higher performance framework because my current rails app is using way to much RAM. Thus my eyes went to Go. However, without a solid user auth system I can't convert to Go. To make your own user auth system is not a very good practice.

答案1

得分: 8

简短回答:不可以。

长回答:Ruby on Rails 是一个框架,Go 是一种编程语言。为 Go 创建一个“通用”的身份验证系统将是一个巨大的任务,或者必须在设计上非常具有观点,因为大多数身份验证系统依赖于会话存储和/或数据库。Rails 可以做到这一点(使用像 Devise 这样的库),因为框架的部分,如 ActiveRecord 和 ActionController,提供了 Devise 可以与之通信的抽象 API。

在大多数情况下,你需要将几个库绑在一起以获得所需的功能。将这些组件“粘合”在一起是一种常见的做法,而庞大的、集成了所有功能的框架通常不被青睐。

以下是一些库的建议:

这些库不能为你提供不同的用户类型,也不能为你编写查询等。我发现使用服务器端会话、一个单独的用户表以及一个包装“需要身份验证”路由并检查会话的 HTTP 中间件,然后将用户重定向到登录页面(并保存当前 URL 以便在重定向后返回)的方式,编写自己的“两个用户级别”(普通用户 vs. 管理员)身份验证相当简单。

英文:

Short answer: no.

Long answer: Ruby on Rails is a framework, Go is a language. Making a "universal" authentication system for Go would be a huge task and/or would have to be very opinionated by design, as most auth systems rely on a session store and/or database. Rails can do this (with libs. like Devise) because parts of the framework like ActiveRecord and ActionController provide an abstract API that Devise can talk to.

For the most part, you'll need to tie together a few libraries to get what you need w/ Go. "Gluing" things together is a commonly preferred way to do things, and monolithic/kitchen sink frameworks aren't typically favoured.

Some suggestions for libraries:

These won't give you distinct user types, write your queries for you, etc. I found writing my own "two user level" (regular vs. admin) auth to be fairly straightforward using server-side sessions, a single User table and a piece of HTTP middleware that wraps "need auth" routes and inspects the session, else it re-directs users to a login page (and saves the current URL for a re-direct after).

huangapple
  • 本文由 发表于 2015年3月17日 17:34:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/29095603.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定