Go to content Go to menu

Large service providers like Google are already offering two way authentication for their services. Others like Microsoft will follow. This authentication mechanism is based on One Time Passwords OTP combined with a timing factor resulting in Timebased One Time Passwords TOTP specified in RFC-6238.

But it’s also possible to provide two factor authentication for self written services. The provided package can be found in the following repositiory:

MCHttpRepository
	location: 'http://www.min.at/prinz/repo/totp'
	user: ''
	password: ''

It provides a simple way to create and validate TOTP’s for a given time or a time window.

" Enter the shared secret here. This is for example "
" the secret you will get if  you change your Gmail "
" account to two way authentication "
key := TOTP decodeBase32: 'qqmholtfsmddokpy'.
" Instantiate a new tokenprovider with a 30 seconds "
" time window "
tp := TOTP createWithSecret: key StepSeconds: 30.
" Calculates a new one time password which "
" changes every #StepSeconds seconds "
token := tp calculateOneTimePassword.
" Validate a token "
valid := TOTP verifyOneTimePassword: token Secret: key.

The larger the StepSeconds parameter the longer the generated tokens won’t change and the greater the time difference between client and server can be. The algorithm uses UTC for client and server times. 30 Seconds time step means that a new token is generated every 30 seconds and that a token is considered valid 30 seconds before and 30 seconds after the point in time it was created.

So change your Google account today and use Pharo to calculate the tokens.

Playing around with Zinc’s streaming entities I looked for a source of streaming data. A never ending streaming source are video streams. So I implemented a very simple Motion JPEG reader using Pharo Smalltalk. It does not use any plugins. Just plain smalltalk and a stock pharo image.

Use your pharos monticello browser to add the following repository and open it.

MCHttpRepository
   location: 'http://www.min.at/prinz/repo/mjpeg'
   user: ''
   password: ''

Update:
Some people have reported hat they cannot load the package into their image. Opening a monticello browser, adding a new http repository with +Repository and pasting the code snippet above and then opening the repository works as expected. Alternatively you can download the package using this link and open it from a local directory repository.

Update 2:
As noted by Sven Van Caekenberghe the mime type for .mcz monticello archives is now set to application/x-monticello in the webserver response. So now there should not be any format errors. Thanks for this hint.

Update 3:
Cédrick Béler modified the source to work under Pharo 7. Thanks for that. The new version can be found in the repo.

It consists of just one class Mjpeg.

| cam |
" Create a new MJPEG stream from a given url "
cam := Mjpeg 
   fromUrl: 'http://218.219.210.174:86/mjpg/video.mjpg?camera=1'.
" Start reading the stream and displaying it in a window "
cam start. 
" Start capturing the stream frame by frame to disk "
cam startCapture: 'c:/temp/test'.
" Returns true if the stream is started (reading or "
" capturing), false otherwise "
cam isRunning.
" Stop reading the stream and close the camera window "
cam stop.
" Stop capturing "
cam stopCapture.

There are two example class methods availabe.

" Sample of a webcam stream from Tokyo "
Mjpeg exampleTokyo.
" Webcam from Vienna "
Mjpeg exampleVienna.

Performance of this simple implementation is quite good. The Tokyo example used the stream available at http://218.219.210.174:86/mjpg/video.mjpg?camera=1. Playing this stream with Pharo has no impact on CPU usage whereas playing the same stream with Chrome has a noticeable effect on CPU usage.



This post describes the Cosm smalltalk package which allows it to send sensor measurements to a cosm.com stream using either Pharo or Squeak Smalltalk.

It’s also possible to use this package with squeak on your Raspberry PI so you can create a data logger with Smalltalk instead of Python or shell scripts as described here.

Use your smalltalks monticello browser to add the following repository and open the repository.

MCHttpRepository
	location: 'http://www.min.at/prinz/repo/cosm'
	user: ''
	password: ''

Inside you will find two packages. One for Squeak (4.x) and one for Pharo (1.4; 2.x).

First you should already have a cosm.com registration. If not register it’s easy and free. Cosm.com allows you to create so called feeds. A feed represents a collection of one or more streams. Streams are used to store values which can be charted on your cosm.com dashboard. So for example you can register a feed MyFeed and inside this feed a stream called Temperature. Now using the Cosm Smalltalk package you are able to send temperature measurements to your Temperature stream.

| client stream result ]
" Create a client instance with your cosm.com API key "
" and feed id "
client := CosmClient 
     newApiKey: 'this should be your API key' 
     feedID: 12345.
" Specify to which stream you want to send data and "
" in which format. Available formats are #JSON, #CSV "
" and #XML "
stream := client 
     openStream: 'my stream name' 
     withFormat: #JSON.
" Send a value (42) to the stream once "
result := stream put: 42.
" Take a block, execute it every x seconds (10) and send "
" the result of it to a cosm.com stream. This represents "
" an automatic stream "
stream put: [ 255 atRandom ] every: 10 
	align: false.
 
" Check if a stream is running automatically "
stream isRunning.
" Stop an automatic running stream "
stream stop.

When using automatic stream mode the time window can be controlled by every: which controls how often data is sent in seconds and align: which specifies if the time window should be time aligned. If set to false the time window starts after the block is evaluated. If set to true the time window is aligned based on how long the block took to evaluate. For example:

Aligned StartAt TimeWindow Block evaluation Next run
true 21:10:10 10 sec. 5 sec. 21:10:25
false 21:10:10 10 sec. 5 sec. 21:10:20
true 21:10:10 10 sec. 13 sec. 21:10:30
false 21:10:10 10 sec. 13 sec. 21:10:33

 
Running the following code

stream put: [ 255 atRandom ] every: 10 
	align: false.

produces random results between 0 and 255 sent to a stream resulting in a simmilar graphs like this:


cosm-stream.png

So now you can send your measurements into the cloud.