aboutsummaryrefslogtreecommitdiff
path: root/internal/googlereader/middleware.go
blob: 1bfc1a6e0bd8d1fb87a75852c0ff23e560ecf188 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

package googlereader // import "miniflux.app/v2/internal/googlereader"

import (
	"context"
	"crypto/hmac"
	"crypto/sha1"
	"encoding/hex"
	"net/http"
	"strings"

	"miniflux.app/v2/internal/http/request"
	"miniflux.app/v2/internal/http/response"
	"miniflux.app/v2/internal/http/response/json"
	"miniflux.app/v2/internal/logger"
	"miniflux.app/v2/internal/model"
	"miniflux.app/v2/internal/storage"
)

type middleware struct {
	store *storage.Storage
}

func newMiddleware(s *storage.Storage) *middleware {
	return &middleware{s}
}

func (m *middleware) clientLogin(w http.ResponseWriter, r *http.Request) {
	clientIP := request.ClientIP(r)
	var username, password, output string
	var integration *model.Integration
	err := r.ParseForm()
	if err != nil {
		logger.Error("[GoogleReader][Login] [ClientIP=%s] Could not parse form", clientIP)
		json.Unauthorized(w, r)
		return
	}
	username = r.Form.Get("Email")
	password = r.Form.Get("Passwd")
	output = r.Form.Get("output")

	if username == "" || password == "" {
		logger.Error("[GoogleReader][Login] [ClientIP=%s] Empty username or password", clientIP)
		json.Unauthorized(w, r)
		return
	}

	if err = m.store.GoogleReaderUserCheckPassword(username, password); err != nil {
		logger.Error("[GoogleReader][Login] [ClientIP=%s] Invalid username or password: %s", clientIP, username)
		json.Unauthorized(w, r)
		return
	}

	logger.Info("[GoogleReader][Login] [ClientIP=%s] User authenticated: %s", clientIP, username)

	if integration, err = m.store.GoogleReaderUserGetIntegration(username); err != nil {
		logger.Error("[GoogleReader][Login] [ClientIP=%s] Could not load integration: %s", clientIP, username)
		json.Unauthorized(w, r)
		return
	}

	m.store.SetLastLogin(integration.UserID)

	token := getAuthToken(integration.GoogleReaderUsername, integration.GoogleReaderPassword)
	logger.Info("[GoogleReader][Login] [ClientIP=%s] Created token: %s", clientIP, token)
	result := login{SID: token, LSID: token, Auth: token}
	if output == "json" {
		json.OK(w, r, result)
		return
	}
	builder := response.New(w, r)
	builder.WithHeader("Content-Type", "text/plain; charset=UTF-8")
	builder.WithBody(result.String())
	builder.Write()
}

func (m *middleware) token(w http.ResponseWriter, r *http.Request) {
	clientIP := request.ClientIP(r)

	if !request.IsAuthenticated(r) {
		logger.Error("[GoogleReader][Token] [ClientIP=%s] User is not authenticated", clientIP)
		json.Unauthorized(w, r)
		return
	}
	token := request.GoolgeReaderToken(r)
	if token == "" {
		logger.Error("[GoogleReader][Token] [ClientIP=%s] User does not have token: %s", clientIP, request.UserID(r))
		json.Unauthorized(w, r)
		return
	}
	logger.Info("[GoogleReader][Token] [ClientIP=%s] token: %s", clientIP, token)
	w.Header().Add("Content-Type", "text/plain; charset=UTF-8")
	w.WriteHeader(http.StatusOK)
	w.Write([]byte(token))
}

func (m *middleware) handleCORS(next http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		w.Header().Set("Access-Control-Allow-Origin", "*")
		w.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS")
		w.Header().Set("Access-Control-Allow-Headers", "Authorization")
		if r.Method == http.MethodOptions {
			w.WriteHeader(http.StatusOK)
			return
		}
		next.ServeHTTP(w, r)
	})
}

func (m *middleware) apiKeyAuth(next http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		clientIP := request.ClientIP(r)

		var token string
		if r.Method == http.MethodPost {
			err := r.ParseForm()
			if err != nil {
				logger.Error("[GoogleReader][Login] [ClientIP=%s] Could not parse form", clientIP)
				Unauthorized(w, r)
				return
			}
			token = r.Form.Get("T")
			if token == "" {
				logger.Error("[GoogleReader][Auth] [ClientIP=%s] Post-Form T field is empty", clientIP)
				Unauthorized(w, r)
				return
			}
		} else {
			authorization := r.Header.Get("Authorization")

			if authorization == "" {
				logger.Error("[GoogleReader][Auth] [ClientIP=%s] No token provided", clientIP)
				Unauthorized(w, r)
				return
			}
			fields := strings.Fields(authorization)
			if len(fields) != 2 {
				logger.Error("[GoogleReader][Auth] [ClientIP=%s] Authorization header does not have the expected structure GoogleLogin auth=xxxxxx - '%s'", clientIP, authorization)
				Unauthorized(w, r)
				return
			}
			if fields[0] != "GoogleLogin" {
				logger.Error("[GoogleReader][Auth] [ClientIP=%s] Authorization header does not begin with GoogleLogin - '%s'", clientIP, authorization)
				Unauthorized(w, r)
				return
			}
			auths := strings.Split(fields[1], "=")
			if len(auths) != 2 {
				logger.Error("[GoogleReader][Auth] [ClientIP=%s] Authorization header does not have the expected structure GoogleLogin auth=xxxxxx - '%s'", clientIP, authorization)
				Unauthorized(w, r)
				return
			}
			if auths[0] != "auth" {
				logger.Error("[GoogleReader][Auth] [ClientIP=%s] Authorization header does not have the expected structure GoogleLogin auth=xxxxxx - '%s'", clientIP, authorization)
				Unauthorized(w, r)
				return
			}
			token = auths[1]
		}

		parts := strings.Split(token, "/")
		if len(parts) != 2 {
			logger.Error("[GoogleReader][Auth] [ClientIP=%s] Auth token does not have the expected structure username/hash - '%s'", clientIP, token)
			Unauthorized(w, r)
			return
		}
		var integration *model.Integration
		var user *model.User
		var err error
		if integration, err = m.store.GoogleReaderUserGetIntegration(parts[0]); err != nil {
			logger.Error("[GoogleReader][Auth] [ClientIP=%s] token: %s", clientIP, token)
			logger.Error("[GoogleReader][Auth] [ClientIP=%s] No user found with the given google reader username: %s", clientIP, parts[0])
			Unauthorized(w, r)
			return
		}
		expectedToken := getAuthToken(integration.GoogleReaderUsername, integration.GoogleReaderPassword)
		if expectedToken != token {
			logger.Error("[GoogleReader][Auth] [ClientIP=%s] Token does not match: %s", clientIP, token)
			Unauthorized(w, r)
			return
		}
		if user, err = m.store.UserByID(integration.UserID); err != nil {
			logger.Error("[GoogleReader][Auth] [ClientIP=%s] No user found with the userID: %d", clientIP, integration.UserID)
			Unauthorized(w, r)
			return
		}

		m.store.SetLastLogin(integration.UserID)

		ctx := r.Context()
		ctx = context.WithValue(ctx, request.UserIDContextKey, user.ID)
		ctx = context.WithValue(ctx, request.UserTimezoneContextKey, user.Timezone)
		ctx = context.WithValue(ctx, request.IsAdminUserContextKey, user.IsAdmin)
		ctx = context.WithValue(ctx, request.IsAuthenticatedContextKey, true)
		ctx = context.WithValue(ctx, request.GoogleReaderToken, token)

		next.ServeHTTP(w, r.WithContext(ctx))
	})
}

func getAuthToken(username, password string) string {
	token := hex.EncodeToString(hmac.New(sha1.New, []byte(username+password)).Sum(nil))
	token = username + "/" + token
	return token
}