Compare commits
6
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
c305b53f11 | ||
|
|
349ab612d6 | ||
|
|
c37a433984 | ||
|
|
8a8ebcdeec | ||
|
|
ab78dd711f | ||
|
|
1fddaf3f67 |
@@ -0,0 +1,72 @@
|
|||||||
|
package auth
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/rand"
|
||||||
|
"encoding/base64"
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"astraltech.xyz/calendar/v2/webserver"
|
||||||
|
"golang.org/x/oauth2"
|
||||||
|
)
|
||||||
|
|
||||||
|
func generateState() (string, error) {
|
||||||
|
b := make([]byte, 32)
|
||||||
|
if _, err := rand.Read(b); err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return base64.RawURLEncoding.EncodeToString(b), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func HandlePasswordAuth(authData webserver.PasswordAuthData) bool {
|
||||||
|
fmt.Printf("%s, %s\n", authData.Username, authData.Password)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func HandleOAuth(authData webserver.OAuthAuthData) bool {
|
||||||
|
state, err := generateState()
|
||||||
|
if err != nil {
|
||||||
|
http.Error(*authData.ResponseWriter, "Failed to generate state", http.StatusInternalServerError)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
verifier := oauth2.GenerateVerifier()
|
||||||
|
|
||||||
|
http.SetCookie(*authData.ResponseWriter, &http.Cookie{
|
||||||
|
Name: "oauth_state",
|
||||||
|
Value: state,
|
||||||
|
Path: "/",
|
||||||
|
MaxAge: 300,
|
||||||
|
HttpOnly: true,
|
||||||
|
SameSite: http.SameSiteLaxMode,
|
||||||
|
})
|
||||||
|
|
||||||
|
http.SetCookie(*authData.ResponseWriter, &http.Cookie{
|
||||||
|
Name: "pkce_verifier",
|
||||||
|
Value: verifier,
|
||||||
|
Path: "/",
|
||||||
|
HttpOnly: true,
|
||||||
|
SameSite: http.SameSiteLaxMode,
|
||||||
|
})
|
||||||
|
|
||||||
|
url := OAuthConfigs[0].AuthCodeURL(
|
||||||
|
state,
|
||||||
|
oauth2.S256ChallengeOption(verifier),
|
||||||
|
)
|
||||||
|
http.Redirect(*authData.ResponseWriter, authData.Request, url, http.StatusFound)
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
func HandleAuthRequest(authType webserver.AuthStyle, authData webserver.AuthData) bool {
|
||||||
|
if authType == webserver.AuthStylePassword {
|
||||||
|
passwordAuthData, _ := authData.(webserver.PasswordAuthData)
|
||||||
|
return HandlePasswordAuth(passwordAuthData)
|
||||||
|
}
|
||||||
|
|
||||||
|
if authType == webserver.AuthStyleOAuth {
|
||||||
|
passwordAuthData, _ := authData.(webserver.OAuthAuthData)
|
||||||
|
return HandleOAuth(passwordAuthData)
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
@@ -0,0 +1,31 @@
|
|||||||
|
package auth
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
|
||||||
|
"github.com/coreos/go-oidc/v3/oidc"
|
||||||
|
"golang.org/x/oauth2"
|
||||||
|
)
|
||||||
|
|
||||||
|
var OAuthConfigs []oauth2.Config
|
||||||
|
|
||||||
|
func CreateTestOAuth() {
|
||||||
|
ctx := context.Background()
|
||||||
|
|
||||||
|
provider, err := oidc.NewProvider(ctx, "https://account.astraltech.xyz/application/o/stalwart/")
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
oauthConfig := oauth2.Config{
|
||||||
|
ClientID: "stalwart-webui",
|
||||||
|
RedirectURL: "http://localhost:8080/callback",
|
||||||
|
Endpoint: provider.Endpoint(),
|
||||||
|
Scopes: []string{
|
||||||
|
oidc.ScopeOpenID,
|
||||||
|
"profile",
|
||||||
|
"email",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
OAuthConfigs = append(OAuthConfigs, oauthConfig)
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
package auth
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
// this is usually served at /callback
|
||||||
|
func OAuthCallback(w http.ResponseWriter, r *http.Request) {
|
||||||
|
fmt.Printf("Getting my OAuth callback")
|
||||||
|
}
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
package auth
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"os"
|
||||||
|
|
||||||
|
"golang.org/x/oauth2"
|
||||||
|
)
|
||||||
|
|
||||||
|
type TokenStore struct {
|
||||||
|
filePath string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ts *TokenStore) Save(token *oauth2.Token) error {
|
||||||
|
data, err := json.Marshal(token)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return os.WriteFile(ts.filePath, data, 0600)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ts *TokenStore) Load() (*oauth2.Token, error) {
|
||||||
|
data, err := os.ReadFile(ts.filePath)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
var token oauth2.Token
|
||||||
|
if err := json.Unmarshal(data, &token); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return &token, nil
|
||||||
|
}
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package main
|
package caldav
|
||||||
|
|
||||||
type MultiStatus struct {
|
type MultiStatus struct {
|
||||||
Responses []Response `xml:"response"`
|
Responses []Response `xml:"response"`
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package main
|
package caldav
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/xml"
|
"encoding/xml"
|
||||||
@@ -21,9 +21,11 @@ const neededXML string = `
|
|||||||
`
|
`
|
||||||
|
|
||||||
var client *http.Client
|
var client *http.Client
|
||||||
|
var serverURL string
|
||||||
|
|
||||||
func init_dav_client() {
|
func InitDavClient(url string) {
|
||||||
client = &http.Client{}
|
client = &http.Client{}
|
||||||
|
serverURL = url
|
||||||
}
|
}
|
||||||
|
|
||||||
type Event struct {
|
type Event struct {
|
||||||
@@ -42,10 +44,10 @@ type SimpleCalDavData struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// this makes stalwart assumptions
|
// this makes stalwart assumptions
|
||||||
func get_caldav_data(username string, password string) SimpleCalDavData {
|
func GetCalDAVData(username string, password string) SimpleCalDavData {
|
||||||
req, _ := http.NewRequest(
|
req, _ := http.NewRequest(
|
||||||
"PROPFIND",
|
"PROPFIND",
|
||||||
serverConfig.URL+"/dav/cal/"+convert_username_into_url(username),
|
serverURL+"/dav/cal/"+convert_username_into_url(username),
|
||||||
strings.NewReader(neededXML),
|
strings.NewReader(neededXML),
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -78,7 +80,7 @@ func get_caldav_data(username string, password string) SimpleCalDavData {
|
|||||||
|
|
||||||
var new_cal Calandar = Calandar{
|
var new_cal Calandar = Calandar{
|
||||||
DisplayName: davResp.Responses[i].PropStat[0].Prop.DisplayName,
|
DisplayName: davResp.Responses[i].PropStat[0].Prop.DisplayName,
|
||||||
Link: serverConfig.URL + davResp.Responses[i].Href,
|
Link: serverURL + davResp.Responses[i].Href,
|
||||||
}
|
}
|
||||||
|
|
||||||
calandars = append(calandars, new_cal)
|
calandars = append(calandars, new_cal)
|
||||||
@@ -107,7 +109,7 @@ const XML_cal string = `
|
|||||||
</C:calendar-query>
|
</C:calendar-query>
|
||||||
`
|
`
|
||||||
|
|
||||||
func get_calandar_data(cal Calandar, username string, password string) Calandar {
|
func GetCalendarData(cal Calandar, username string, password string) Calandar {
|
||||||
req, _ := http.NewRequest(
|
req, _ := http.NewRequest(
|
||||||
"REPORT",
|
"REPORT",
|
||||||
cal.Link,
|
cal.Link,
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
package main
|
package caldav
|
||||||
|
|
||||||
import "strings"
|
import "strings"
|
||||||
|
|
||||||
@@ -2,4 +2,9 @@ module astraltech.xyz/calendar/v2
|
|||||||
|
|
||||||
go 1.26.1
|
go 1.26.1
|
||||||
|
|
||||||
require github.com/arran4/golang-ical v0.3.5 // indirect
|
require (
|
||||||
|
github.com/arran4/golang-ical v0.3.5 // indirect
|
||||||
|
github.com/coreos/go-oidc/v3 v3.20.0 // indirect
|
||||||
|
github.com/go-jose/go-jose/v4 v4.1.4 // indirect
|
||||||
|
golang.org/x/oauth2 v0.36.0 // indirect
|
||||||
|
)
|
||||||
|
|||||||
@@ -1,2 +1,8 @@
|
|||||||
github.com/arran4/golang-ical v0.3.5 h1:bbz6ld4dC+MmCKiFfOd6SkmIGnhNMBACZ485ULh7p9A=
|
github.com/arran4/golang-ical v0.3.5 h1:bbz6ld4dC+MmCKiFfOd6SkmIGnhNMBACZ485ULh7p9A=
|
||||||
github.com/arran4/golang-ical v0.3.5/go.mod h1:OnguFgjN0Hmx8jzpmWcC+AkHio94ujmLHKoaef7xQh8=
|
github.com/arran4/golang-ical v0.3.5/go.mod h1:OnguFgjN0Hmx8jzpmWcC+AkHio94ujmLHKoaef7xQh8=
|
||||||
|
github.com/coreos/go-oidc/v3 v3.20.0 h1:EtE0WIBHk03N+DqGkY4+UONzzZHk7amKt6IyNd7OsZE=
|
||||||
|
github.com/coreos/go-oidc/v3 v3.20.0/go.mod h1:DYCf24+ncYi+XkIH97GY1+dqoRlbaSI26KVTCI9SrY4=
|
||||||
|
github.com/go-jose/go-jose/v4 v4.1.4 h1:moDMcTHmvE6Groj34emNPLs/qtYXRVcd6S7NHbHz3kA=
|
||||||
|
github.com/go-jose/go-jose/v4 v4.1.4/go.mod h1:x4oUasVrzR7071A4TnHLGSPpNOm2a21K9Kf04k1rs08=
|
||||||
|
golang.org/x/oauth2 v0.36.0 h1:peZ/1z27fi9hUOFCAZaHyrpWG5lwe0RJEEEeH0ThlIs=
|
||||||
|
golang.org/x/oauth2 v0.36.0/go.mod h1:YDBUJMTkDnJS+A4BP4eZBjCqtokkg1hODuPjwiGPO7Q=
|
||||||
|
|||||||
+6
-39
@@ -1,54 +1,21 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"astraltech.xyz/calendar/v2/auth"
|
||||||
"strings"
|
"astraltech.xyz/calendar/v2/caldav"
|
||||||
|
|
||||||
"astraltech.xyz/calendar/v2/webserver"
|
"astraltech.xyz/calendar/v2/webserver"
|
||||||
ics "github.com/arran4/golang-ical"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func HandleAuthRequest(username string, password string) bool {
|
|
||||||
calDavData := get_caldav_data(username, password)
|
|
||||||
|
|
||||||
fmt.Printf("Users display name: %s\n", calDavData.DisplayName)
|
|
||||||
fmt.Printf("Calandar Count: %d\n", len(calDavData.Calandars))
|
|
||||||
for i := range len(calDavData.Calandars) {
|
|
||||||
calDavData.Calandars[i] = get_calandar_data(calDavData.Calandars[i], username, password)
|
|
||||||
|
|
||||||
fmt.Printf("Cal %d is named %s\n", i, calDavData.Calandars[i].DisplayName)
|
|
||||||
fmt.Printf("\tLink to cal: %s\n", calDavData.Calandars[i].Link)
|
|
||||||
fmt.Printf("\tEvent count: %d\n", len(calDavData.Calandars[i].Events))
|
|
||||||
for j := range len(calDavData.Calandars[i].Events) {
|
|
||||||
cal, err := ics.ParseCalendar(strings.NewReader(calDavData.Calandars[i].Events[j].CalData))
|
|
||||||
if err != nil {
|
|
||||||
fmt.Print(err.Error())
|
|
||||||
}
|
|
||||||
events := cal.Events()
|
|
||||||
for k := range len(events) {
|
|
||||||
name := events[k].GetProperty(ics.ComponentProperty(ics.PropertySummary)).Value
|
|
||||||
start, _ := events[k].GetStartAt()
|
|
||||||
end, _ := events[k].GetEndAt()
|
|
||||||
time := end.Sub(start)
|
|
||||||
fmt.Printf("\t\tEvent name: %s\n", name)
|
|
||||||
fmt.Printf("\t\t\tEvent start date: %s\n", end.UTC().String())
|
|
||||||
fmt.Printf("\t\t\tEvent length date: %s\n", time.String())
|
|
||||||
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
auth.CreateTestOAuth()
|
||||||
read_config()
|
read_config()
|
||||||
init_dav_client()
|
caldav.InitDavClient(serverConfig.URL)
|
||||||
|
|
||||||
webserver.ServeLoginPage("/login", webserver.CustomizableLoginData{
|
webserver.ServeLoginPage("/login", webserver.CustomizableLoginData{
|
||||||
ServiceName: "Astral Calendar",
|
ServiceName: "Astral Calendar",
|
||||||
AuthRequestFunction: HandleAuthRequest,
|
AuthRequestFunction: auth.HandleAuthRequest,
|
||||||
})
|
})
|
||||||
|
webserver.ServeWebpage("/callback", auth.OAuthCallback)
|
||||||
webserver.EnableLogoRoute()
|
webserver.EnableLogoRoute()
|
||||||
webserver.EnableStaticRoute()
|
webserver.EnableStaticRoute()
|
||||||
webserver.ServeWebserver()
|
webserver.ServeWebserver()
|
||||||
|
|||||||
+35
-2
@@ -10,10 +10,28 @@ type LoginPageData struct {
|
|||||||
IsHiddenClassList string
|
IsHiddenClassList string
|
||||||
LoginData CustomizableLoginData
|
LoginData CustomizableLoginData
|
||||||
}
|
}
|
||||||
|
type AuthStyle int
|
||||||
|
|
||||||
|
const (
|
||||||
|
AuthStylePassword AuthStyle = 0
|
||||||
|
AuthStyleOAuth AuthStyle = 1
|
||||||
|
)
|
||||||
|
|
||||||
|
type AuthData interface{}
|
||||||
|
|
||||||
|
type PasswordAuthData struct {
|
||||||
|
Username string
|
||||||
|
Password string
|
||||||
|
}
|
||||||
|
|
||||||
|
type OAuthAuthData struct {
|
||||||
|
ResponseWriter *http.ResponseWriter
|
||||||
|
Request *http.Request
|
||||||
|
}
|
||||||
|
|
||||||
type CustomizableLoginData struct {
|
type CustomizableLoginData struct {
|
||||||
ServiceName string
|
ServiceName string
|
||||||
AuthRequestFunction func(string, string) bool
|
AuthRequestFunction func(AuthStyle, AuthData) bool
|
||||||
}
|
}
|
||||||
|
|
||||||
var LoginPageDataCustomizations CustomizableLoginData
|
var LoginPageDataCustomizations CustomizableLoginData
|
||||||
@@ -27,15 +45,30 @@ func loginHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if r.Method == http.MethodPost {
|
if r.Method == http.MethodPost {
|
||||||
|
action := r.FormValue("action")
|
||||||
|
|
||||||
|
if action == "password" {
|
||||||
username := r.FormValue("username")
|
username := r.FormValue("username")
|
||||||
if strings.Contains(username, "/") {
|
if strings.Contains(username, "/") {
|
||||||
tmpl.Execute(w, LoginPageData{IsHiddenClassList: "", LoginData: LoginPageDataCustomizations})
|
tmpl.Execute(w, LoginPageData{IsHiddenClassList: "", LoginData: LoginPageDataCustomizations})
|
||||||
}
|
}
|
||||||
password := r.FormValue("password")
|
password := r.FormValue("password")
|
||||||
|
|
||||||
auth_success := LoginPageDataCustomizations.AuthRequestFunction(username, password)
|
auth_success := LoginPageDataCustomizations.AuthRequestFunction(AuthStylePassword, PasswordAuthData{
|
||||||
|
Username: username,
|
||||||
|
Password: password,
|
||||||
|
})
|
||||||
|
if auth_success == false {
|
||||||
|
tmpl.Execute(w, LoginPageData{IsHiddenClassList: "", LoginData: LoginPageDataCustomizations})
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
auth_success := LoginPageDataCustomizations.AuthRequestFunction(AuthStyleOAuth, OAuthAuthData{
|
||||||
|
ResponseWriter: &w,
|
||||||
|
Request: r,
|
||||||
|
})
|
||||||
if auth_success == false {
|
if auth_success == false {
|
||||||
tmpl.Execute(w, LoginPageData{IsHiddenClassList: "", LoginData: LoginPageDataCustomizations})
|
tmpl.Execute(w, LoginPageData{IsHiddenClassList: "", LoginData: LoginPageDataCustomizations})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -23,17 +23,20 @@
|
|||||||
|
|
||||||
<div>
|
<div>
|
||||||
<label class="input_label">Username</label><br />
|
<label class="input_label">Username</label><br />
|
||||||
<input type="text" name="username" placeholder="" required />
|
<input type="text" name="username" placeholder="" />
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div>
|
<div>
|
||||||
<label class="input_label">Password</label><br />
|
<label class="input_label">Password</label><br />
|
||||||
<div class="password_box">
|
<div class="password_box">
|
||||||
<input type="password" name="password" placeholder="" required />
|
<input type="password" name="password" placeholder="" />
|
||||||
<button type="button" class="show_password_toggle closed"></button>
|
<button type="button" class="show_password_toggle closed"></button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<button type="submit">Login</button>
|
<div id="login_buttons">
|
||||||
|
<button type="submit" name="action" value="password">Login with password</button>
|
||||||
|
<button type="submit" id="login_with_oauth" name="action" value="oauth"><img src="/favicon.ico"/></button>
|
||||||
|
</div>
|
||||||
</form>
|
</form>
|
||||||
|
|
||||||
<script src="/static/error/error.js" type="text/javascript"></script>
|
<script src="/static/error/error.js" type="text/javascript"></script>
|
||||||
|
|||||||
@@ -33,3 +33,23 @@
|
|||||||
font-size: 1.25rem;
|
font-size: 1.25rem;
|
||||||
margin-bottom: 8px;
|
margin-bottom: 8px;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#login_buttons {
|
||||||
|
display: grid;
|
||||||
|
grid-gap: 5px;
|
||||||
|
grid-template-columns: 1fr 43px;
|
||||||
|
}
|
||||||
|
|
||||||
|
#login_with_oauth {
|
||||||
|
padding: 0 !important;
|
||||||
|
position: relative;
|
||||||
|
}
|
||||||
|
|
||||||
|
#login_with_oauth img {
|
||||||
|
position: absolute;
|
||||||
|
width: 30px;
|
||||||
|
height: 30px;
|
||||||
|
top: 50%;
|
||||||
|
left: 50%;
|
||||||
|
transform: translateX(-50%) translateY(-50%);
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user