Using Swagger to access your secured api

2017-04-18 21:01:00 +0000

Swagger, http://swagger.io/ is a great way make your api accessible. Not only documentation is provided, but your users, your co-developers or your self can try out your api. But what if your API is secured using a token based security system like webtokens?

Well, it’s quite easy to modify your swagger frontend to allow adding any token to requests.

First somewhere in your swagger template file, add the following:

            <form onsubmit="addApiKeyAuthorization()">
                <div class="input">
                    <input placeholder="access_token" id="input_apiKey" name="input_apiKey" type="text">
                    <input type="submit" value="Authenticate"/>
                </div>
            </form>

Then in the header add the following Javascript

<script>
    function addApiKeyAuthorization(){
        var apiKey = getUrlVars().input_apiKey;

        if (apiKey && apiKey.trim() != "") {
            console.log("initialzing oauth via api-key")
            var apiKeyAuth = new SwaggerClient.ApiKeyAuthorization("Authorization", "Bearer " + apiKey, "header");
            window.swaggerUi.api.clientAuthorizations.add("bearer", apiKeyAuth );
            //For clarity, also add in the input field
            $('#input_apiKey')[0].value=apiKey
        }
    }

</script>

Read more

Purging files from Git history

2016-10-14 16:01:00 +0000

Most developers, like me, use Git for versioning. Even for personal projects, it’s great to have a history of files available. Deleting a file in git will cause it to remove from your working directory, while the file is still present in the history. Usually this is what you want, but sometimes you might want to remove a file completely including it’s history.

There’s a command for that but typically for git it’s not exactly intuitive. To alter history, you can use the filter-branch command. Filter branch can be supplied with a filter, which is a shell command that’s executed on each commit.

  • To purge a file from history, as if it never existed execute:

    git filter-branch -f --index-filter 'git rm --cached --ignore-unmatch MYFILE public' HEAD

    Where you can replace MYFILE with one or more files you want to delete.

  • If you want to purge a whole directory, including it’s content, add the -r switch:

    git filter-branch -f --index-filter 'git rm --cached --ignore-unmatch MYDIRECTORY public' HEAD

Great if you checked in a large file, or file in a public repository that shouldn’t be public. For more information, read the git manual or the numerous tutorials that exist. For example, git-filter can also be used to move a file from one repository to another while preserving history.

Read more

Archive

subscribe via RSS