volatiletech / authboss

The boss of http auth.
MIT License
3.85k stars 207 forks source link

Need for backends to hook up to(MySQL, Postgres, mongo, etc.) #259

Closed siredwin closed 4 years ago

siredwin commented 4 years ago

Great package. I am slowly implementing this for my project. I just realize that this project could benefit from having a backends section. It would make it truly plug and play as I am sure they are almost going to be implemented the same way.

aarondl commented 4 years ago

Yep. They're all going to be quite similar. Downside being you can't guarantee table/column names so you may end up needing configuration at which point just providing a copy/paste template implementation is probably more useful than some complicated mini-library that has a bunch of configuration in it.

siredwin commented 4 years ago

One more thing. On the examples, there is a commented section under NewFromOAuth2. Do I need that part? It looks like a user is returned if available on creating new.

aarondl commented 4 years ago

Sorry I'm not sure where/what you're referring to. This project's not all in my head these days.

siredwin commented 4 years ago

The commented section. It looks redundant for Oath2(Gmail login)

// NewFromOAuth2 creates an oauth2 user (but not in the database, just a blank one to be saved later)
func (m MemStorer) NewFromOAuth2(ctx context.Context, provider string, details map[string]string) (authboss.OAuth2User, error) {
    switch provider {
    case "google":
        email := details[aboauth.OAuth2Email]

        var user *User
        if u, ok := m.Users[email]; ok {
            user = &u
        } else {
            user = &User{}
        }

        // Google OAuth2 doesn't allow us to fetch real name without more complicated API calls
        // in order to do this properly in your own app, look at replacing the authboss oauth2.GoogleUserDetails
        // method with something more thorough.
        user.Name = "Unknown"
        user.Email = details[aboauth.OAuth2Email]
        user.OAuth2UID = details[aboauth.OAuth2UID]
        user.Confirmed = true

        return user, nil
    }

    return nil, errors.Errorf("unknown provider %s", provider)
}

// SaveOAuth2 user
func (m MemStorer) SaveOAuth2(ctx context.Context, user authboss.OAuth2User) error {
    u := user.(*User)
    m.Users[u.Email] = *u

    return nil
}

/*
func (s MemStorer) PutOAuth(uid, provider string, attr authboss.Attributes) error {
    return s.Create(uid+provider, attr)
}
func (s MemStorer) GetOAuth(uid, provider string) (result interface{}, err error) {
    user, ok := s.Users[uid+provider]
    if !ok {
        return nil, authboss.ErrUserNotFound
    }
    return &user, nil
}
func (s MemStorer) AddToken(key, token string) error {
    s.Tokens[key] = append(s.Tokens[key], token)
    fmt.Println("AddToken")
    spew.Dump(s.Tokens)
    return nil
}
func (s MemStorer) DelTokens(key string) error {
    delete(s.Tokens, key)
    fmt.Println("DelTokens")
    spew.Dump(s.Tokens)
    return nil
}
func (s MemStorer) UseToken(givenKey, token string) error {
    toks, ok := s.Tokens[givenKey]
    if !ok {
        return authboss.ErrTokenNotFound
    }
    for i, tok := range toks {
        if tok == token {
            toks[i], toks[len(toks)-1] = toks[len(toks)-1], toks[i]
            s.Tokens[givenKey] = toks[:len(toks)-1]
            return nil
        }
    }
    return authboss.ErrTokenNotFound
}
func (s MemStorer) ConfirmUser(tok string) (result interface{}, err error) {
    fmt.Println("==============", tok)
    for _, u := range s.Users {
        if u.ConfirmToken == tok {
            return &u, nil
        }
    }
    return nil, authboss.ErrUserNotFound
}
func (s MemStorer) RecoverUser(rec string) (result interface{}, err error) {
    for _, u := range s.Users {
        if u.RecoverToken == rec {
            return &u, nil
        }
    }
    return nil, authboss.ErrUserNotFound
}
*/
aarondl commented 4 years ago

All you need to do is implement the required interfaces. See the documentation in the OAuth2 package (https://godoc.org/github.com/volatiletech/authboss/oauth2) as well as the docs for the OAuth2 storage interface: https://godoc.org/github.com/volatiletech/authboss#OAuth2Provider https://godoc.org/github.com/volatiletech/authboss#OAuth2ServerStorer

Nothing else is required. This is part of Authbosses modular design.