Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
caddy
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
caddy
Commits
96ae288c
Commit
96ae288c
authored
Oct 17, 2015
by
Matthew Holt
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
More refactoring; cleaning up code, preparing for tests
parent
a3a82657
Changes
3
Expand all
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
186 additions
and
164 deletions
+186
-164
config/letsencrypt/letsencrypt.go
config/letsencrypt/letsencrypt.go
+133
-121
config/letsencrypt/storage.go
config/letsencrypt/storage.go
+0
-40
config/letsencrypt/user.go
config/letsencrypt/user.go
+53
-3
No files found.
config/letsencrypt/letsencrypt.go
View file @
96ae288c
This diff is collapsed.
Click to expand it.
config/letsencrypt/storage.go
View file @
96ae288c
...
...
@@ -86,43 +86,3 @@ func emailUsername(email string) string {
}
return
email
[
:
at
]
}
/*
// StorageDir is the full path to the folder where this Let's
// Encrypt client will set up camp. In other words, where it
// stores user account information, keys, and certificates.
// All files will be contained in a 'letsencrypt' folder
// within StorageDir.
//
// Changing this after the program has accessed this folder
// will result in undefined behavior.
var StorageDir = "."
// Values related to persisting things on the file system
const (
// ContainerDir is the name of the folder within StorageDir
// in which files or folders are placed.
ContainerDir = "letsencrypt"
// File that contains information about the user's LE account
UserRegistrationFile = "registration.json"
)
// BaseDir returns the full path to the base directory in which
// files or folders may be placed, e.g. "<StorageDir>/letsencrypt".
func BaseDir() string {
return filepath.Join(StorageDir, ContainerDir)
}
// AccountsDir returns the full path to the directory where account
// information is stored for LE users.
func AccountsDir() string {
return filepath.Join(BaseDir(), "users")
}
// AccountsDir gets the full path to the directory for a certain
// user with the email address email.
func AccountDir(email string) string {
return filepath.Join(AccountsDir(), email)
}
*/
config/letsencrypt/user.go
View file @
96ae288c
package
letsencrypt
import
(
"bufio"
"crypto/rand"
"crypto/rsa"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"strings"
"github.com/mholt/caddy/server"
"github.com/xenolf/lego/acme"
)
...
...
@@ -29,6 +33,7 @@ func (u User) GetPrivateKey() *rsa.PrivateKey {
}
// getUser loads the user with the given email from disk.
// If the user does not exist, it will create a new one.
func
getUser
(
email
string
)
(
User
,
error
)
{
var
user
User
...
...
@@ -59,7 +64,7 @@ func getUser(email string) (User, error) {
}
// saveUser persists a user's key and account registration
// to the file system.
// to the file system.
It does NOT register the user via ACME.
func
saveUser
(
user
User
)
error
{
// make user account folder
err
:=
os
.
MkdirAll
(
storage
.
User
(
user
.
Email
),
0700
)
...
...
@@ -84,8 +89,10 @@ func saveUser(user User) error {
}
// newUser creates a new User for the given email address
// with a new private key. This function does not register
// the user via ACME.
// with a new private key. This function does NOT save the
// user to disk or register it via ACME. If you want to use
// a user account that might already exist, call getUser
// instead.
func
newUser
(
email
string
)
(
User
,
error
)
{
user
:=
User
{
Email
:
email
}
privateKey
,
err
:=
rsa
.
GenerateKey
(
rand
.
Reader
,
rsaKeySize
)
...
...
@@ -95,3 +102,46 @@ func newUser(email string) (User, error) {
user
.
key
=
privateKey
return
user
,
nil
}
// getEmail does everything it can to obtain an email
// address from the user to use for TLS for cfg. If it
// cannot get an email address, it returns empty string.
func
getEmail
(
cfg
server
.
Config
)
string
{
// First try the tls directive from the Caddyfile
leEmail
:=
cfg
.
TLS
.
LetsEncryptEmail
if
leEmail
==
""
{
// Then try memory (command line flag or typed by user previously)
leEmail
=
DefaultEmail
}
if
leEmail
==
""
{
// Then try to get most recent user email ~/.caddy/users file
// TODO: Probably better to open the user's json file and read the email out of there...
userDirs
,
err
:=
ioutil
.
ReadDir
(
storage
.
Users
())
if
err
==
nil
{
var
mostRecent
os
.
FileInfo
for
_
,
dir
:=
range
userDirs
{
if
!
dir
.
IsDir
()
{
continue
}
if
mostRecent
==
nil
||
dir
.
ModTime
()
.
After
(
mostRecent
.
ModTime
())
{
mostRecent
=
dir
}
}
if
mostRecent
!=
nil
{
leEmail
=
mostRecent
.
Name
()
}
}
}
if
leEmail
==
""
{
// Alas, we must bother the user and ask for an email address
reader
:=
bufio
.
NewReader
(
os
.
Stdin
)
fmt
.
Print
(
"Email address: "
)
// TODO: More explanation probably, and show ToS?
var
err
error
leEmail
,
err
=
reader
.
ReadString
(
'\n'
)
if
err
!=
nil
{
return
""
}
DefaultEmail
=
leEmail
}
return
strings
.
TrimSpace
(
leEmail
)
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment