From 3af91d7cfdb334e602d312743a89e64cd2d369ee Mon Sep 17 00:00:00 2001 From: ᴜɴᴋɴᴡᴏɴ Date: Sun, 20 Sep 2020 11:19:02 +0800 Subject: auth: decouple types and functions from db (#6320) --- internal/auth/github/provider.go | 57 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 internal/auth/github/provider.go (limited to 'internal/auth/github/provider.go') diff --git a/internal/auth/github/provider.go b/internal/auth/github/provider.go new file mode 100644 index 00000000..4add2e54 --- /dev/null +++ b/internal/auth/github/provider.go @@ -0,0 +1,57 @@ +// Copyright 2020 The Gogs Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package github + +import ( + "strings" + + "gogs.io/gogs/internal/auth" +) + +// Provider contains configuration of a PAM authentication provider. +type Provider struct { + config *Config +} + +// NewProvider creates a new PAM authentication provider. +func NewProvider(cfg *Config) auth.Provider { + return &Provider{ + config: cfg, + } +} + +func (p *Provider) Authenticate(login, password string) (*auth.ExternalAccount, error) { + fullname, email, website, location, err := p.config.doAuth(login, password) + if err != nil { + if strings.Contains(err.Error(), "401") { + return nil, auth.ErrBadCredentials{Args: map[string]interface{}{"login": login}} + } + return nil, err + } + return &auth.ExternalAccount{ + Login: login, + Name: login, + FullName: fullname, + Email: email, + Location: location, + Website: website, + }, nil +} + +func (p *Provider) Config() interface{} { + return p.config +} + +func (p *Provider) HasTLS() bool { + return true +} + +func (p *Provider) UseTLS() bool { + return true +} + +func (p *Provider) SkipTLSVerify() bool { + return p.config.SkipVerify +} -- cgit v1.2.3