HaskerDeux

Check-in [e53b9e78fb]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Enable auto-login if token has expired

Note: I am aware this is terribly unhaskelly, but making it work was my
main aim, making it less terrible will come. There's also a fair bit of
repetition here.

This tries to get a list of todos whatever the command and checks the
response for an invalid csrf token, if that occurs it removes the file
and calls login again.

This means that if you are actually calling for a list of todos it ends
up doing it twice which is a bit rubbish really. It would be nicer for
it to:

1. Call whatever command is asked for
2. Catch a invalid_csrf_token in the response
3. Re-login
4. Continue to call the original command

It might never be that good, but I do hope at least to get rid of all
the if/then/else though.

Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | origin/issue1 | trunk | master
Files: files | file ages | folders
SHA3-256: e53b9e78fb39186f1eefd0009bb26e7ccd6ce39adeb66bfd3781cf0918ab93e2
User & Date: base@atomicules.co.uk 2017-10-22 10:44:02
Context
2017-10-22
10:56
Sort out markup in README check-in: a393b4c91b user: base@atomicules.co.uk tags: master, origin/issue1, trunk
10:44
Enable auto-login if token has expired

Note: I am aware this is terribly unhaskelly, but making it work was my
main aim, making it less terrible will come. There's also a fair bit of
repetition here.

This tries to get a list of todos whatever the command and checks the
response for an invalid csrf token, if that occurs it removes the file
and calls login again.

This means that if you are actually calling for a list of todos it ends
up doing it twice which is a bit rubbish really. It would be nicer for
it to:

1. Call whatever command is asked for
2. Catch a invalid_csrf_token in the response
3. Re-login
4. Continue to call the original command

It might never be that good, but I do hope at least to get rid of all
the if/then/else though. check-in: e53b9e78fb user: base@atomicules.co.uk tags: master, origin/issue1, trunk

2017-02-26
15:02
Enable commands for any date

Previously Haskerdeux worked around the assumption of "today" and
although you could move todos to any date, view todos for tomorrow and
put off things until tomorrow there was no way to, say, delete a todo on
a random date or list todos from a selected day next week.

This does change the syntax a little bit. Supplying a date argument is
now compulsory, but it "understands" the dates of "today" and
"tomorrow".

The command "today" is now called "todos" since it can list todos for
any date. The command "tomorrow" has been removed similarly. Variables
have also been renamed to suit (`todays_date` is now `todos_date`). check-in: ab44c0ddfb user: base@atomicules.co.uk tags: master, origin/issue1, trunk

Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to haskerdeux.hs.

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
	let authtokenword = stripPrefix "value=\"" $ last authwords
	let revauthtokenword = reverse $ fromJust authtokenword
	let authtoken = reverse $ fromJust $ stripPrefix ">\"" revauthtokenword
	return authtoken


login [username, password] = do
	--See if we have a token, then to clear we can just delete the file
	--TODO: handle that error
	home <- getHomeDirectory
	check <- doesFileExist (home ++ "/.haskerdeux-token")










	if check
		then


			readFile (home ++ "/.haskerdeux-token")

		else do
			body <- readProcess "curl" ["-s", "-L", "-c", "haskerdeux.cookies", "https://teuxdeux.com/login"] []
			token <- getauthtoken body
			writeFile (home ++ "/.haskerdeux-token") token
			--can probably use one post?
			let curlheader = "X-CSRF-Token: " ++ token
			let curlpostfields = "username=" ++ username ++ "&password=" ++ password ++ "&authenticity_token=" ++ token
			body <- readProcess "curl" ["-s", "-L", "-c", "haskerdeux.cookies", "-b", "haskerdeux.cookies", "-H", curlheader, "-d", curlpostfields, "https://teuxdeux.com/login"] []
			return token


todos (token, [todos_date]) = do







<
<

|
>
>
>
>
>
>
>
>
>
>
|
|
>
>
|
>




<







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
	let authtokenword = stripPrefix "value=\"" $ last authwords
	let revauthtokenword = reverse $ fromJust authtokenword
	let authtoken = reverse $ fromJust $ stripPrefix ">\"" revauthtokenword
	return authtoken


login [username, password] = do


	home <- getHomeDirectory
	savedtoken <- doesFileExist (home ++ "/.haskerdeux-token")
	--Need to make a query here and check token is still valid
	--I am aware this is horribly unhaskelly and I plan on sorting that.
	if savedtoken
		then do
			token <- readFile (home ++ "/.haskerdeux-token")
			time <- getClockTime >>= toCalendarTime --https://wiki.haskell.org/Unix_tools/Date
			let date = formatCalendarTime defaultTimeLocale "%Y-%m-%d" time
			let curlheader = "X-CSRF-Token: " ++ token
			body <- readProcess "curl" ["-s", "-L", "-c", "haskerdeux.cookies", "-b", "haskerdeux.cookies", "-H", curlheader, "https://teuxdeux.com/api/v1/todos/calendar?begin_date="++date++"&end_date="++date] []
			let ok = not ("Invalid CSRF Token" `isInfixOf` body)
			if ok
				then
					return token
				else do
					removeFile (home ++ "/.haskerdeux-token")
					login [username, password]
		else do
			body <- readProcess "curl" ["-s", "-L", "-c", "haskerdeux.cookies", "https://teuxdeux.com/login"] []
			token <- getauthtoken body
			writeFile (home ++ "/.haskerdeux-token") token

			let curlheader = "X-CSRF-Token: " ++ token
			let curlpostfields = "username=" ++ username ++ "&password=" ++ password ++ "&authenticity_token=" ++ token
			body <- readProcess "curl" ["-s", "-L", "-c", "haskerdeux.cookies", "-b", "haskerdeux.cookies", "-H", curlheader, "-d", curlpostfields, "https://teuxdeux.com/login"] []
			return token


todos (token, [todos_date]) = do