Archive for February, 2015

OAuth2 Credentials into Google

Tuesday, February 24th, 2015

GoogleAppEngine.jpg

I'm not a fan of OAuth2... not in the least. It's excessively complicated, it requires call-backs, and in general it's no more secure than anything else, it's just more complicated. Add to that there's no really good library for it as the Google folks keep changing things, and you have something that's always going to require hacks... always going to require fixing, and never going go provide a seamless way to authenticate on a remote system.

But that's just an opinion. I have had to make it work at The Shop, and when I finally got it to work, I wasn't about to let this evaporate into the ether... I needed to make a gist of it, and document what I was doing so that I could come back to this and be able to remember it all at a later date.

The State of OAuth2 Clojure Libraries

The first really depressing thing was that there seemed to be no decent OAuth2 libraries for Clojure. And while there seemed to be a lot of forks of the clj-oauth2 library - but many, like the original, were years old - and they didn't work. Not even close. Now I'm not silly enough to think that the spec changed, but I do believe that Google changed things on it's end to make it more secure, and in so doing, broke all the clj-oauth2 work, and it's derivatives.

Still, there is the code I can look at. And some have pulled in the features that are needed, and so it's not impossible to make this thing work... though it's likely to take a lot of time.

The project.clj File

When I was able to get something working, I made a gist of all the important files, so that I could include them here as reference. I also wanted to post the link to the #clojure room in IRC because one of the guys there gave me a hint as to which library to use. He wasn't right, but he was close, and that's all I needed.

The project.clj file has all the versions of the libraries I used:

What I found was that this version of clj-oauth2 had the most complete mapping of the data coming from Google, which included the expires-in time - which I think I still may be able to put to really good use soon. While it didn't have the functions to renew the access-token, it turns out that it's not hard to write, and I pulled that from another fork of the master project.

The server.clj File

OAuth2 still requires that the user go to Google on a redirect, and then the call-back from them is where we get the first bit of the authentication data. I'm not convinced that this is at all necessary, but it's how things are. Given that, we needed to have the server.clj have an endpoint /google that gets redirected to the right place at Google for the user to login and accept the app.

There is also the callback endpoint, and then a few that return the token data, and renew the token. Nothing special, really, but the targets for the OAuth2 are really important, and it's just sad that we have to have them in the first place.

The dcm.clj File

The final piece is really the meat of the problem.

We start off with the Missing Functions in the clj-oauth2 library, and then jump right into the static config for our application. These are all generated by Google, and you can get them from Google when you register your project/client.

We then have the authentication and re-authentication functions, which took an enormous amount of time to get right, but don't look overly complex in the least. Lovely.

Finally, we have a few calls to test that we got the user profile information properly, and that we can make subsequent calls to Google and get the data requested. It's not a lot, but it works, and it proves that things are working up to that point.

In the end, I'm glad I have it all done, and I'll be integrating the Custos Server in as a secret store of the credentials soon. Then I'll be using redis as the back-end and then pulling data from Google and loading it there. All this is a complete, stand-alone, back-end data collector for the ad messaging data for a client from Google.

Not bad...

Every Now and Then… SPICE

Wednesday, February 18th, 2015

MacSpice

Every now and then I remember all the work I did in college, and at Auburn University with SPICE and MAGIC. These are tools that I just can't ever forget, and all the time I spent on the terminals in the Terminal Rooms in the EE building... well... yes, let's just say they were some very Good Times. And of course, that was a good 30 years ago. Wow. In the subsequent years, MacSpice is around, and it's got all the models, and all the capabilities that the old SPICE had - plus some amazing plotting.

In fact, I'm sure my retina MacBook Pro is probably more powerful than all the computers the EE Dept. had in 1988, and it shows. I can start with this very simple LPF circuit:

  Four double-pole, low-pass, LC-filters
  VIN 1 0 AC 1
  * Q = 0.5
  R1 1 2 200
  L1 2 3 10mH
  C1 3 0 1uF
  * Q = 1
  R2 1 4 100
  L2 4 5 10mH
  C2 5 0 1uF
  * Q = 2
  R3 1 6 50
  L3 6 7 10mH
  C3 7 0 1uF
  * Q = 4
  R4 1 8 25
  L4 8 9 10mH
  C4 9 0 1uF
  .AC DEC 100 100hz 10khz
  .END

and then in the MacSpice console:

  MacSpice 1 -> source lpf.ckt
 
  Circuit: Four double-pole, low-pass, LC-filters
 
  MacSpice 2 -> run
  On line 2:
      vin 1 0 ac 1
      Warning: Source 'vin' has no DC value specified, 0V assumed.
  MacSpice 3 -> plot vm(3) vm(5) vm(7) vm(9)

and we see:

Four LPFs

It really is amazing what these tools can do, and they continue to bring a smile to my face. What a treat.

Postman in Chrome is a Wonderful Tool

Wednesday, February 4th, 2015

Google Chrome

I've been doing a little work on the Clojure CryptoQuip solver by adding a new RESTful endpoint for the server that takes the quip and the clue and solves the puzzle. It's not all that hard, but it's a POST call, as the body of the POST is JSON, and I needed a tool to hit my server to make sure that I had it all working. Enter Postman.

This is just an amazing tool for Chrome. It's a very singular task, and it does an excellent job of making sure that you can make all the calls you need with all the headers and arguments and get back what you need - and even keep a running history of all the calls to re-do should you need them.

Clearly the folks that wrote this knew what they needed and spent quite a bit of time on it. I have yet to find a hole, a bug, or a limitation. It's just good, solid working code. And it's free. Wow. Impressive.

I looked at several of the OS X tools on the App Store, and even on the web and the nice ones were more than $20, and most weren't nearly as nice as Postman. If you need something like this, you need it, and this is as good a tool for the job as you can get.

Having Fun with a Paralegal

Wednesday, February 4th, 2015

PHB.gif

OK, this isn't something that typically happens, but today I got an email from the paralegal assigned to my lawyer for my divorce. Typically, I hate getting emails because it's either bad news, or a bill. Some would argue they are one in the same, but I see the bills as slightly less depressing than the "regular" bad news I get about the divorce.

So I got an empty email from her, so I wrote back:

This email is empty - I have no idea what this is about.

Please provide me a little context.

and she replied:

Ha! I was adding to my previous email that I could email you record (in several sections) AND deliver the paper copies so you don’t have to print it out and you will still have electronic copies. OK?

I had no idea whatsoever what she was talking about, but clearly she thought this was funny. She still didn't know that she was talking to the wrong person. So I went along with the joke, and wrote back:

I’m sure to you this all makes perfect sense, but to me I’m still thinking “What is this thing that Beth wants to get to me?” and “Why do I need paper copies and electronic ones?”

Maybe it’s an invitation to a party?

Maybe it’s a movie - Oh… I do love movies. Spy ones are good… I like Comedy too.

Maybe it’s a sandwich? No… well… maybe - a paper sandwich?

So I must not have communicated clearly… my fault… I do that a lot.

Let’s try again...

What is this email about?

What do you need to get to me? And why?

Is it important, or just informational?

I think if you answer these, I’ll have a lot better idea what you’re talking about. No guarantees, mind you, but I think I’ll have a better handle on things.

I could go on, and we did for another round of emails until she realized that she was mailing to me, and not another "Bob B" that she needed to be emailing. Then she picked up the phone and was quite apologetic. I thought it was at least a little amusing, but clearly she seemed to think the lawyer(s) would not find this comical - in the least.

I feel for her. An honest mistake is going to be a source of grief and embarrassment for her for the next day or so. I understand that lawyers are meant to be flawless, but we're all people, and we all make mistakes - big and small. I hope they don't give her too hard a time.

CryptoQuip Solver in Swift

Tuesday, February 3rd, 2015

Swift

OK, I've spent several hours with Swift, and it's pretty close to Ruby, but it's not the same in that the String is still a class, and that means that it doesn't have the duality that a string in Ruby - or clojure - has. This makes a lot of the functions in the CryptoQuip solver a lot harder.

Little things, too - like finding the distinct characters in a String. In clojure it's easy:

  (count (distinct "rabbit"))
  => 5

and even ruby it's nearly as easy:

  "rabbit".chars.to_a.uniq.count
  => 5

but in Swift, it's nasty:

  func distinct(list: [Character]) -> [Character]  {
    var buff = Dictionary<Character, Character>()
    for x in list {
      buff[x] = x
    }
    return buff.keys.array
  }
 
  let src = Array("rabbit")
  distinct(src).count

I did find a StackOverflow idea that does it, but it's a function that basically builds up a new list by adding one element at a time, and then checking before each add that the new element doesn't already exist in the collection. Since both these collections are Arrays, this is a linear search over and over and over again, and it's a mess.

I'm sure Swift has a ton of nice features, and it's quoted as faster than ObjC - which is nice, but issues like this make me realize it's a 1.x language and that the builders haven't had the time to go into the same use-cases that they have for other code bases, and fill in all these kinds of features that will make it a lot easier to write more complex systems.

For now, I'm content to hold off on more Swift work. It's just not where I'd like it to be.

UPDATE: I added in the function that I wrote, and it works, but Holy Cow! it's a mess. You can't easily make it one call:

  distinct(Array("rabbit")).count

because you have to worry about the mutability of the Array, and that causes warnings in the code... it's just not ready for all the things you need to do. Soon, hopefully.

I wish I didn’t care…

Tuesday, February 3rd, 2015

Path

Today I got another email from my lawyers about more pre-trial motion paperwork for the divorce. I don't know why this is an ongoing issue, but I have a feeling it's because Liza doesn't like the idea of getting less than she thinks she needs - or deserves. But the interesting thing is that it doesn't matter one little bit. Not one.

My wife of (now) 29 years can choose to divorce me without any consideration or cost to her. She can ditch me like a old shoe. I can't say a thing, I can't stop here in the least. And more importantly, she has the law on her side. There is no financial cost to her of this divorce - she gets awarded money not because she deserves it - but because someone decided that there are sufficient numbers of bad husbands out there that a woman should be able to divorce the worthless slug, and then be granted a portion of his money for the rest of time.

So I get these emails about giving them more financial information, and it just breaks my heart. I hate digging this up, and it makes me about as mad as I can be. But in the end - again - it doesn't matter one whit. The legal system in this state is not about justice and fair, it's about empowerment for women, and while at some level I understand that, I don't see that a judge should not look at the circumstances of the divorce, and say "Hey, you just got tired of this person, so you don't get all you want..."

But as I said... none of this matters at all. And it makes me sick. And I wish I didn't care at all. But it still breaks my heart.

Swift Dictionaries are Always Value Types

Tuesday, February 3rd, 2015

Swift

More reading in The Swift Programming Language and I've come to the part about Arrays and Dictionaries. Specifically, a dictionary is treated as a Valu e, and that means that it's copied on every assignment and on all function or method calls. The significance of this, to me, is profound. One of the singularly most useful data structures for a coder is the dictionary (the other is the array), and to find out that passing around a dictionary makes a copy on each method call and on each assignment.

In the CryptoQuip codebase, we create a map of all the known words - where the key is the pattern of the word, and the value is an array of all known words with that pattern. As we run through the list of words (a file), we calculate it's pattern, and then add it to the array in the dictionary for that pattern. In clojure, we really compute all the patterns for the words, and then group them by their patterns.

We then pass this dictionary around to make it simple to find the possible words that a cypher text word could be. If the docs are as they have been written:

Whenever you assign a Dictionary instance to a constant or variable, or pass a Dictionary instance as an argument to a function or method call, the dictionary is copied at the point that the assignment or call takes place.

yet they also try to say this isn't an issue:

The descriptions below refer to the “copying” of arrays, dictionaries, strings, and other values. Where copying is mentioned, the behavior you see in your code will always be as if a copy took place. However, Swift only performs an actual copy behind the scenes when it is absolutely necessary to do so. Swift manages all value copying to ensure optimal performance, and you should not avoid assignment to try to preempt this optimization.

Sadly, StackOverflow isn't a lot of help, and this includes talking to someone in the Swift Language group in Apple. The advice appears to be: Wrap it in a class.

The reason being that all Classes are pass-by-reference as opposed to pass-by-value. While this certainly can solve the problem, it makes something that should be very simple a lot harder. I can see how to do this, and should we move ahead with the CryptoQuip solver in Swift, it can be done, but it seems like a hack.

Kinda seeing some of the cracks in the Swift language... not ideal, but it is what it is.

Swift Structures are Always Pass-by-Value

Tuesday, February 3rd, 2015

Swift

I've been reading The Swift Programming Language this morning just to stay sharp, and because I've read several accounts of where Swift out performs ObjC, and the ruby-like nature of Swift means that it might be a lot faster to code up. So I was reading more...

What I found was pretty surprising about the differences between Structures and Classes - Structures are always copied when they are passed around in the code. This means that there is no way to pass-by-reference a struct to a function or method, and this will certainly modify where they can be used.

As a counter-point, in C++, classes and structs are basically the same thing. There are very minor differences, at best, and yet here in Swift, it's a fundamental difference that they chose to implement, as opposed to had to implement, and that tells me that there's something they see in Structures that they don't see for classes.

Not quite sure what that is, but immutability could certainly be one thing - passing by value allows the value(s) to be modified, but the caller's values are never touched. There might also be something in the NSPoint structures as well - something where they are looking at very simple data elements with no methods on them. Yet this means you can't have a function that modifies a passed-in structure.

Very interesting. Not sure where they are going with this, but it's something to look out for.

Metra Changes – Just Keepin’ it Fresh

Tuesday, February 3rd, 2015

Metra Engine

This month Metra had another rate hike, and it was a pretty good sized one - I've heard that it was more than $15/month for most of the folks I talked to. It was 10.8%, and they didn't have one 'no' vote on the measure. From Naperville, it's now $185.25, and that's getting above my first car payment, but it's still way cheaper than a car, and I can't even imagine driving into the city on a daily basis.

So it's the cost of living in Naperville and working in Chicago. Maybe someday I'll be able to work at home again, and I'll be able to skip the monthly ticket, but for now, I need to come to Chicago, and I'm used to it.