alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Play Framework/Scala example source code file (ApplicationSecretGenerator.scala)

This example Play Framework source code file (ApplicationSecretGenerator.scala) is included in my "Source Code Warehouse" project. The intent of this project is to help you more easily find Play Framework (and Scala) source code examples by using tags.

All credit for the original source code belongs to Play Framework; I'm just trying to make examples easier to find. (For my Scala work, see my Scala examples and tutorials.)

Play Framework tags/keywords

applicationsecret, applicationsecretgenerator, did, file, generated, none, play, play framework, plugin, replacing, sbt, some, string, updating

The ApplicationSecretGenerator.scala Play Framework example source code

/*
 * Copyright (C) 2009-2014 Typesafe Inc. <http://www.typesafe.com>
 */
package play.sbtplugin

import java.security.SecureRandom
import sbt._

/**
 * Provides tasks for generating and updating application secrets
 */
object ApplicationSecretGenerator {

  def generateSecret = {
    val random = new SecureRandom()

    (1 to 64).map { _ =>
      (random.nextInt(75) + 48).toChar
    }.mkString.replaceAll("\\\\+", "/")
  }

  def generateSecretTask = Def.task[String] {
    val secret = generateSecret
    Keys.streams.value.log.info("Generated new secret: " + secret)
    secret
  }

  private val ApplicationSecret = """\s*application\.secret\s*[=:].*""".r

  def updateSecretTask = Def.task[File] {
    val secret: String = play.PlayImport.PlayKeys.generateSecret.value
    val baseDir: File = Keys.baseDirectory.value
    val log = Keys.streams.value.log

    val secretConfig = s"""application.secret="$secret""""

    val applicationConf = Option(System.getProperty("config.file")).getOrElse("conf/application.conf")
    val appConfFile = new File(baseDir, applicationConf)

    if (appConfFile.exists()) {
      log.info("Updating application secret in " + appConfFile.getCanonicalPath)
      val lines = IO.readLines(appConfFile)

      val appSecret = lines.find(ApplicationSecret.pattern.matcher(_).matches())

      val newLines = appSecret match {
        case Some(line) =>
          log.info("Replacing old application secret: " + line)
          lines.map {
            case `line` => secretConfig
            case other => other
          }
        case None =>
          log.warn("Did not find application secret in " + appConfFile.getCanonicalPath)
          log.warn("Adding application secret to start of file")
          secretConfig :: lines
      }

      IO.writeLines(appConfFile, newLines)

      appConfFile
    } else {
      log.error("Could not find configuration file at " + appConfFile.getCanonicalPath)
      throw new FeedbackProvidedException {}
    }
  }

}

Other Play Framework source code examples

Here is a short list of links related to this Play Framework ApplicationSecretGenerator.scala source code file:

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.